您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何使用yii2上傳圖片,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
第一步: 搭建上傳類基礎工作,具體請看://www.jb51.net/article/120242.htm
第二步:建站一個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(); $('<div>').appendTo($('body')).attr({"class":"onedialog",'id':"oneupload"}); $('<iframe>').appendTo($('#oneupload')).attr({"src":"?r=upload","class":"oneiframe"}) }); var v=$('#product-picurl').val(); if(v){ $('<img>').attr({"src":v,"style":"height:50px"}).insertAfter($('#product-picurl')); } });
然后在frontend\assets\AppAsset.php中注冊這兩個文件
class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = [ 'css/site.css', 'css/main.css', ]; public $js = [ 'assets/main.js' ]; public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ]; }
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, 'file'); if ($model->file && $model->validate()) { //$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); $fileName='uploads/' . date("YmdHis") . '.' . $model->file->extension; $model->file->saveAs($fileName); } echo "<script src='assets/upload.js'></script>;"; echo "<script>"; echo "var oneinput=parent.document.getElementById('product-picurl');"; echo "parent.document.getElementById('product-picurl').value='".$fileName."';"; echo "var oneupload = parent.document.getElementById('oneupload');"; echo "var img = document.createElement('img');"; echo "img.setAttribute('style', 'height:50px');"; echo "img.src ='".$fileName."';"; echo "insertAfter(img,oneinput);"; echo "oneupload.parentNode.removeChild(oneupload)"; echo "</script>"; } return $this->render('upload', ['model' => $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 [ [['file'], 'file'], ]; } } ?>
upload.php
<?php use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?> <?= $form->field($model, 'file')->fileInput() ?> <button>Submit</button> <?php ActiveForm::end() ?>
關于如何使用yii2上傳圖片就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。