当我尝试上传多个文件时,我无法执行此操作
模型文件
namespace app\models;
class ImageFiles extends \yii\base\Model
{
public $imageFiles;
public function rules()
{
return [
[['imageFiles'], 'file', 'skipOnEmpty'=>false, 'extensions'=>'jpg, png', 'maxFiles'=>4]
];
}
}
查看档案
use yii\widgets\ActiveForm; $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); echo $form->field($model, 'imageFiles[]')->fileInput(['multiple'=>true, 'accept'=>'image/*']); echo \yii\helpers\Html::submitButton('Submit',['class'=>'btn btn-success']); ActiveForm::end();
控制器文件
namespace app\models;
class ImageFiles extends \yii\base\Model
{
public $imageFiles;
public function rules()
{
return [
[['imageFiles'], 'file', 'skipOnEmpty'=>false, 'extensions'=>'jpg, png', 'maxFiles'=>4]
];
}
}
namespace app\controllers;
use Yii;
class ImageFilesController extends \yii\web\Controller
{
public function actionIndex()
{
$model = new \app\models\ImageFiles();
if(Yii::$app->request->isPost){
$model->imageFiles = \yii\web\UploadedFile::getInstance($model, 'imageFiles');
print_r($model->imageFiles);
return;
}
return $this->render('index', ['model'=>$model]);
}
}
没有显示任何错误,因为print_r($model->imageFiles)
没有任何价值。但是当我使用$model->imageFiles
时,它会打印所有图像细节。我很困惑,我做了yii文档所说的但是它不起作用。
答案 0 :(得分:0)
对于多个文件,请使用UploadedFile::getInstances()
class ImageFilesController extends \yii\web\Controller
{
public function actionIndex()
{
$model = new \app\models\ImageFiles();
if(Yii::$app->request->isPost){
$model->imageFiles = \yii\web\UploadedFile::getInstances($model, 'imageFiles');
print_r($model->imageFiles);
return;
}
return $this->render('index', ['model'=>$model]);
}
}