【发布时间】:2015-08-06 10:39:23
【问题描述】:
控制器
public function actionCreate()
{
$model = new RoomTypes();
if ($model->load(Yii::$app->request->post())) {
// get the uploaded file instance. for multiple file uploads
// the following data will return an array
$image = UploadedFile::getInstance($model, 'image');
// store the source file name
$model->room_type = $image->name;
$imageName_1 = $model->room_type;
$ext = end((explode(".", $image->name)));
// generate a unique file name
$model->pic_1 = Yii::$app->security->generateRandomString().".{$ext}";
// the path to save file, you can set an uploadPath
// in Yii::$app->params (as used in example below)
$path = Yii::$app->params['uploadPath'] . $model->pic_1;
if($model->save()){
$image->saveAs($path);
return $this->redirect(['view', 'id'=>$model->_id]);
} else {
// error in saving model
}
}
return $this->render('create', [
'model'=>$model,
]);
}
型号:
public $image;
[['image'], 'safe'],
[['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
查看
<?= $form->field($model, 'room_type')->label(false)->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'image[]')->widget(FileInput::classname(), [
'options'=>['accept'=>'image/*', 'multiple'=>true],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
'slugCallback' => new JsExpression(function(room_type)
{return room_type;},),]]);
?>
上传文件时出现此错误。 也不接受多张图片上传,这是怎么回事? 这种多文件上传怎么实现???
【问题讨论】:
-
不要创建另一个变量
$image只需使用 [['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles ' => 4],并循环它们以存储多个图像。 -
做了更正,但仍然是同样的错误:(
-
你能不能显示错误...
-
它在控制器在线
$model->room_type = $image->name; -
您必须确保您的
Activeform有一个optionenctype => multipart/form-data用于上传多个文件。