yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传:
yii2单个文件上传:
上传步奏,先创建上传表单模型model(包含验证规则),其次控制器操作action,以及相对应的view:
model层:
Upload.php [单文件上传模型]
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/2/12 * Time: 12:18 */ namespace app\models; use Yii; use yii\base\Model; class Upload extends Model{ public $file; public function rules(){ return [ [['file'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',], ]; } public function attributeLabels(){ return [ 'file'=>'文件上传' ]; } }
UploadForm.php [多文件上传模型]
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/2/13 * Time: 10:39 */ namespace app\models; use Yii; use yii\base\Model; class UploadForm extends Model { /** * @var UploadedFile|Null file attribute */ public $file; /** * @return array the validation rules. */ public function rules() { return [ [['file'], 'file', 'maxFiles' => 10,'extensions'=>'jpg,png,gif'], ]; } public function attributeLabels(){ return [ 'file'=>'多文件上传' ]; } }
Controller层,以TestController中的upload操作和upmore操作
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/2/11 * Time: 16:18 */ namespace app\controllers; use Yii; use yii\web\Controller; use app\models\Upload; use app\models\UploadForm; use yii\web\UploadedFile; class TestController extends Controller{ public function actionIndex(){ return $this->renderPartial('index'); } /** * @return string|\yii\web\Response * 单文件上传 */ public function actionUpload(){ $model= new Upload(); if (Yii::$app->request->isPost) { $file = UploadedFile::getInstance($model, 'file'); $path='uploads/'.date("YmdH",time()).'/'; if ($file && $model->validate()) { if(!file_exists($path)){ mkdir($path,'777'); } $file->saveAs($path . time() . '.' . $file->getExtension()); Yii::$app->session->setFlash('success','上传成功!'); return $this->redirect('upload'); } } return $this->render('upload',['model'=>$model]); } public function actionUpmore(){ $model = new UploadForm(); if (Yii::$app->request->isPost) { $file = UploadedFile::getInstances($model, 'file'); if ($file && $model->validate()) { echo "<pre/>"; foreach ($file as $fl) { $fl->saveAs('uploads/' .mt_rand(1100,9900) .time() .$fl->baseName. '.' . $fl->extension); } Yii::$app->session->setFlash('success','上传成功!'); return $this->redirect('upmore'); } } return $this->render('upmore', ['model' => $model]); } }
view:层
[单文件view uplod.php]
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h4>文件上传</h4> <?php if(Yii::$app->session->hasFlash('success')):?> <div class="alert alert-danger"> <?=Yii::$app->session->getFlash('success')?> </div> <?php endif ?> <?php $form=ActiveForm::begin([ 'id'=>'upload', 'enableAjaxValidation' => false, 'options'=>['enctype'=>'multipart/form-data'] ]); ?> <?= $form->field($model, 'file')->fileInput();?> <?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?> <?php ActiveForm::end(); ?> </body> </html>
[多文件view upmore.php]
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h4>多文件上传</h4> <?php if(Yii::$app->session->hasFlash('success')):?> <div class="alert alert-danger"> <?=Yii::$app->session->getFlash('success')?> </div> <?php endif ?> <?php $form=ActiveForm::begin([ 'id'=>'upload', 'enableAjaxValidation' => false, 'options'=>['enctype'=>'multipart/form-data'] ]); ?> <?= $form->field($model, 'file[]')->fileInput(['multiple' => true]);?> <?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?> <?php ActiveForm::end(); ?> </body> </html>