Yii2.0 多文件上传

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2.0 多文件上传相关的知识,希望对你有一定的参考价值。

---------------------------------------------------------------------------------------------------

首先你得调整模型类,在 file 验证规则里增加一个 maxFiles 选项,用以限制一次上传文件的最大数量。 

upload()方法也得修改, 以便一个一个地保存上传的文件。

---------------------------------------------------------------------------------------------------

<?php
namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm1 extends Model{
    /**
     * @var UploadedFile[]
     */
    public $imageFiles;

    public function rules()
    {
        return [
            [[‘imageFiles‘], ‘file‘, ‘skipOnEmpty‘ => false, ‘extensions‘ => ‘png, jpg‘, ‘maxFiles‘ => 4],
        ];
    }    
    public function upload()
    {
        foreach ($this->imageFiles as $file) {
            $file->saveAs(‘./upload/‘ . $file->baseName . ‘.‘ . $file->extension);
        }
        return true;
    }
}

--------------------------------------------------------------------------------------------------------------

在视图文件里,你需要把 multiple 选项添加到fileInput()函数调用里, 这样文件输入控件就可以接收多个文件。

---------------------------------------------------------------------------------------------------------------

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>

<?php $form = ActiveForm::begin([‘options‘ => [‘enctype‘ => ‘multipart/form-data‘]]) ?>

<table class="table table-bordered table-hover definewidth m10">
<tr>
   <td class="tableleft">上传多个图片</td>
   <td><?= $form->field($model, ‘imageFiles[]‘)->fileInput([‘multiple‘ => true, ‘accept‘ => ‘image/*‘]) ?>

   </td>
</tr>

<tr>
   <td class="tableleft"></td>
   <td>
       <button type="submit" class="btn btn-primary" type="button">保存</button>
   </td>
</tr>

<?php ActiveForm::end() ?>

-------------------------------------------------------------------------------------------------------------

最后,在控制器的 action 方法中,你应该调用 UploadedFile::getInstances() 而不是 UploadedFile::getInstance() 来把UploadedFile 实例数组赋值给 UploadForm::imageFiles

------------------------------------------------------------------------------------------------------------

<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use app\models\UploadForm1;
use yii\web\UploadedFile;

class PhotoController extends Controller
{

    public $enableCsrfValidation = false;
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $model = new UploadForm1();
        if($request->isPost)
        {
            $file=$model->imageFiles = UploadedFile::getInstances($model, ‘imageFiles‘);
            //print_r($file);die;
            foreach ($file as $key => $v) {
                $image[]=$v->name;                
            }
            //print_r($image);die;  
            $images=implode(‘,‘,$image);
            if($model->upload())
            {
                $connection = \Yii::$app->db;
                $connection->createCommand()->insert(‘upload‘, [
                    ‘image‘ => $images,
                ])->execute();
            }          
        }
        else
        {
            return $this->render(‘upload1‘,[‘model‘ => $model]);
        }
    }
}

以上是关于Yii2.0 多文件上传的主要内容,如果未能解决你的问题,请参考以下文章

el-upload多文件上传

iview Upload组件多文件上传,以及vue文件下载

element-ui upload组件多文件上传

el-upload multiple多文件上传,只传上一个的问题

element el-upload自定义上传显示进度条,多文件上传进度

使用iview Upload进行多文件上传,编辑页初始已上传的图片失败的问题