当我尝试上传多个文件时,我不能这样做
Model File
namespace app\models;
class ImageFiles extends \yii\base\Model
{
public $imageFiles;
public function rules()
{
return [
[['imageFiles'], 'file', 'skipOnEmpty'=>false, 'extensions'=>'jpg, png', 'maxFiles'=>4]
];
}
}
View File
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();
Controller File
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
没有任何 Value . 但是当我使用 print_r($_FILES)
时会打印所有图像细节 . 我很困惑,我做了yii文档所说的但是它不起作用 .
1 回答
对于多个文件,请使用UploadedFile::getInstances()