yii2 单文件上传和多文件上传

yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传:


yii2单个文件上传:

上传步奏,先创建上传表单模型model(包含验证规则),其次控制器操作action,以及相对应的view:

model层:

Upload.PHP  [单文件上传模型]

[php]  view plain  copy
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Administrator 
  5.  * Date: 2015/2/12 
  6.  * Time: 12:18 
  7.  */  
  8.   
  9. namespace app\models;  
  10. use Yii;  
  11. use yii\base\Model;  
  12.   
  13. class Upload extends Model{  
  14.     public $file;  
  15.   
  16.   
  17.     public function rules(){  
  18.         return [  
  19.           
  20.             [['file'], 'file''extensions' => 'jpg, png''mimeTypes' => 'image/jpeg, image/png',],  
  21.         ];  
  22.     }  
  23.   
  24.   
  25.     public function attributeLabels(){  
  26.         return [  
  27.             'file'=>'文件上传'  
  28.         ];  
  29.     }  
  30. }   

UploadForm.php  [多文件上传模型]

[php]  view plain  copy
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Administrator 
  5.  * Date: 2015/2/13 
  6.  * Time: 10:39 
  7.  */  
  8.   
  9. namespace app\models;  
  10. use Yii;  
  11. use yii\base\Model;  
  12.   
  13. class UploadForm extends Model  
  14. {  
  15.     /** 
  16.      * @var UploadedFile|Null file attribute 
  17.      */  
  18.     public $file;  
  19.   
  20.     /** 
  21.      * @return array the validation rules. 
  22.      */  
  23.     public function rules()  
  24.     {  
  25.         return [  
  26.             [['file'], 'file''maxFiles' => 10,'extensions'=>'jpg,png,gif'],  
  27.         ];  
  28.     }  
  29.   
  30.     public function attributeLabels(){  
  31.         return [  
  32.             'file'=>'多文件上传'  
  33.         ];  
  34.     }  
  35. }  




Controller层,以TestController中的upload操作和upmore操作

[php]  view plain  copy
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Administrator 
  5.  * Date: 2015/2/11 
  6.  * Time: 16:18 
  7.  */  
  8.   
  9. namespace app\controllers;  
  10. use Yii;  
  11. use yii\web\Controller;  
  12. use app\models\Upload;  
  13. use app\models\UploadForm;  
  14. use yii\web\UploadedFile;  
  15.   
  16. class TestController extends  Controller{  
  17.   
  18.     public function actionIndex(){  
  19.         return  $this->renderPartial('index');  
  20.     }  
  21.   
  22.     /** 
  23.      * @return string|\yii\web\Response 
  24.      * 单文件上传 
  25.      */  
  26.     public function actionUpload(){  
  27.         $modelnew Upload();  
  28.   
  29.         if (Yii::$app->request->isPost) {  
  30.             $file = UploadedFile::getInstance($model'file');  
  31.             $path='uploads/'.date("YmdH",time()).'/';  
  32.             if ($file && $model->validate()) {  
  33.                 if(!file_exists($path)){  
  34.                     mkdir($path,0775,true);
  35.                 }  
  36.                 $file->saveAs($path . time() . '.' . $file->getExtension());  
  37.                 Yii::$app->session->setFlash('success','上传成功!');  
  38.                 return $this->redirect('upload');  
  39.             }  
  40.         }  
  41.   
  42.         return $this->render('upload',['model'=>$model]);  
  43.     }  
  44.   
  45.   
  46.     public function actionUpmore(){  
  47.         $model = new UploadForm();  
  48.         if (Yii::$app->request->isPost) {  
  49.             $file = UploadedFile::getInstances($model'file');  
  50.   
  51.             if ($file && $model->validate()) {  
  52.                 echo "<pre/>";  
  53.   
  54.                 foreach ($file as $fl) {  
  55.                     $fl->saveAs('uploads/' .mt_rand(1100,9900) .time() .$fl->baseName'.' . $fl->extension);  
  56.                 }  
  57.                 Yii::$app->session->setFlash('success','上传成功!');  
  58.                 return $this->redirect('upmore');  
  59.             }  
  60.         }  
  61.   
  62.         return $this->render('upmore', ['model' => $model]);  
  63.     }  
  64. }   


 

view:层

[单文件view  uplod.php]

[php]  view plain  copy
  1. <?php  
  2. use yii\helpers\Html;  
  3. use yii\widgets\ActiveForm;  
  4. ?>  
  5. <!doctype html>  
  6. <html lang="en">  
  7. <head>  
  8.     <meta charset="UTF-8">  
  9.     <title>Document</title>  
  10. </head>  
  11. <body>  
  12. <h4>文件上传</h4>  
  13. <?php if(Yii::$app->session->hasFlash('success')):?>  
  14.     <div class="alert alert-danger">  
  15.     <?=Yii::$app->session->getFlash('success')?>  
  16.     </div>  
  17. <?php endif ?>  
  18. <?php $form=ActiveForm::begin([  
  19.     'id'=>'upload',  
  20.     'enableAjaxValidation' => false,  
  21.     'options'=>['enctype'=>'multipart/form-data']  
  22. ]);  
  23. ?>  
  24. <?= $form->field($model'file')->fileInput();?>  
  25. <?=  Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>  
  26. <?php ActiveForm::end(); ?>  
  27.   
  28. </body>  
  29. </html>  



[多文件view upmore.php]


[php]  view plain  copy
  1. <?php  
  2. use yii\helpers\Html;  
  3. use yii\widgets\ActiveForm;  
  4. ?>  
  5. <!doctype html>  
  6. <html lang="en">  
  7. <head>  
  8.     <meta charset="UTF-8">  
  9.     <title>Document</title>  
  10. </head>  
  11. <body>  
  12. <h4>多文件上传</h4>  
  13. <?php if(Yii::$app->session->hasFlash('success')):?>  
  14.     <div class="alert alert-danger">  
  15.         <?=Yii::$app->session->getFlash('success')?>  
  16.     </div>  
  17. <?php endif ?>  
  18. <?php $form=ActiveForm::begin([  
  19.     'id'=>'upload',  
  20.     'enableAjaxValidation' => false,  
  21.     'options'=>['enctype'=>'multipart/form-data']  
  22. ]);  
  23. ?>  
  24. <?= $form->field($model'file[]')->fileInput(['multiple' => true]);?>  
  25. <?=  Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>  
  26. <?php ActiveForm::end(); ?>  
  27.   
  28. </body>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值