首頁 > php框架 > YII > 主體

yii2 怎麼上傳圖片

爱喝马黛茶的安东尼
發布: 2019-12-07 16:21:15
原創
2327 人瀏覽過

yii2 怎麼上傳圖片

第一步:建立上傳類別基礎工作,具體請看:http://www.yiichina.com/tutorial/328

第二步:建置站一個product表,字段id,name,picurl.

第三步:GII產生PRODUCT 模型,類,視圖。

第四步:

main.css 放在frontend\web\css
.onedialog{position:absolute; left: 300px; top: 500px; z-index: 10; width: 700px; height: 400px;border
-radius:5px;
box-shadow:5px 2px 6px #000; border: 2px solid #666}
.oneiframe{ width: 100%; height: 100% }
登入後複製

main.js 放在frontend\web\assets

$(function(){
$('#product-picurl').click(function(){
$('#oneupload').remove();
$(&#39;<div>&#39;).appendTo($(&#39;body&#39;)).attr({"class":"onedialog",&#39;id&#39;:"oneupload"});
$(&#39;<iframe>&#39;).appendTo($(&#39;#oneupload&#39;)).attr({"src":"?r=upload","class":"oneiframe"})
});
    var v=$(&#39;#product-picurl&#39;).val();
if(v){
$(&#39;<img>&#39;).attr({"src":v,"style":"height:50px"}).insertAfter($(&#39;#product-picurl&#39;));
}
});
登入後複製

然後在frontend\assets\AppAsset.php中註冊這兩個檔案

class AppAsset extends AssetBundle
{
    public $basePath = &#39;@webroot&#39;;
    public $baseUrl = &#39;@web&#39;;
    public $css = [
        &#39;css/site.css&#39;,
        &#39;css/main.css&#39;,
    ];
    public $js = [
        &#39;assets/main.js&#39;
    ];
    public $depends = [
        &#39;yii\web\YiiAsset&#39;,
        &#39;yii\bootstrap\BootstrapAsset&#39;,
    ];
}
登入後複製

UploadController.php

<?PHP
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;
class UploadController extends Controller
{
    public function actionIndex()
    {
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, &#39;file&#39;);
            if ($model->file && $model->validate()) {
                //$model->file->saveAs(&#39;uploads/&#39; . $model->file->baseName . &#39;.&#39; .$model->
                file->extension); 
                $fileName=&#39;uploads/&#39; . date("YmdHis") . &#39;.&#39; . $model->file->extension;
                $model->file->saveAs($fileName);
            }
            echo "<script src=&#39;assets/upload.js&#39;></script>;";
            echo "<script>";
            echo "var oneinput=parent.document.getElementById(&#39;product-picurl&#39;);";
            echo "parent.document.getElementById(&#39;product-picurl&#39;).value=&#39;".$fileName."&#39;;";
            echo "var oneupload = parent.document.getElementById(&#39;oneupload&#39;);";
            echo "var img = document.createElement(&#39;img&#39;);";
            echo "img.setAttribute(&#39;style&#39;, &#39;height:50px&#39;);";
            echo "img.src =&#39;".$fileName."&#39;;";
            echo "insertAfter(img,oneinput);";
            echo "oneupload.parentNode.removeChild(oneupload)";
            echo "</script>";
        }
        return $this->render(&#39;upload&#39;, [&#39;model&#39; => $model]);
    }
}
?>
登入後複製

#UploadForm.php

<?PHP
namespace app\models;
use yii\base\Model;
use yii\web\UploadedFile;
/**
 * UploadForm is the model behind the upload form.
 */
class UploadForm extends Model
{
    /**
     * @var UploadedFile file attribute
     */
    public $file;
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [[&#39;file&#39;], &#39;file&#39;],
        ];
    }
}
?>
登入後複製

upload.php

#
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([&#39;options&#39; => [&#39;enctype&#39; => &#39;multipart/form-data&#39;]]) ?>
<?= $form->field($model, &#39;file&#39;)->fileInput() ?>
<button>Submit</button>
<?php ActiveForm::end() ?>
登入後複製

PHP中文網,有大量免費的Yii入門教學,歡迎大家學習!

以上是yii2 怎麼上傳圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板