2 回答
TA贡献1850条经验 获得超11个赞
问题是模型类中的验证。我已经将它们标记为safe
[['mainImage','productImage'], 'safe'],
我注释掉了这个验证并且它起作用了
TA贡献1828条经验 获得超13个赞
如果要上传多个文件,则需要更改视图
<?= $form->field($model, 'mainImage')->fileInput() ?>
<?= $form->field($model, 'productImage[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
并且您的ruleinmodel说您一直在尝试上传多个图像的属性productImage[]和mainImage. 但是视图表示该属性mainImage用于单个文件,因此更改规则。
[['mainImage'], 'file', 'skipOnEmpty' => true, 'extensions' => 'jpeg, jpg, png'],
[['productImage'], 'file','skipOnEmpty' => true, 'extensions' => 'jpeg, jpg, png','checkExtensionByMimeType'=>false, 'maxFiles'=>10],
在控制器中,
$model->productImage = UploadedFile::getInstances($model, 'productImage');
$model->mainImage = UploadedFile::getInstance($model, 'mainImage');
if ($model->multipleImageUpload()) {
// file is uploaded successfully
return;
}
在你的模型中,
public function multipleImageUpload()
{
if ($this->validate()) {
foreach ($this->imageFiles as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
return true;
} else {
return false;
}
}
如需更多参考,请访问此链接:https : //www.yiiframework.com/doc/guide/2.0/en/input-file-upload
- 2 回答
- 0 关注
- 172 浏览
添加回答
举报