我有一個提交文本輸入和文件輸入的窗體(非活動窗體)。我可以保存文本字段但未檢測到文件。在yii2中上傳沒有活動窗體的文件
查看:
<form id="data" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>bicycle_no</label>
<input class="form-control" name="bicycle_no" placeholder="" type="text" />
</div>
<div class="form-group">
<label>image</label>
<input class="form-control" name="file" type="file">
</div>
<div class="form-group">
<a href="<?=Yii::$app->homeUrl?>/site/index" class="btn btn-danger">Cancel</a>
<button class="btn btn-primary">Save</button>
</div>
</form>
我可以在控制器獲取圖像:
if(!empty($_FILES['file']['name'])) {
//var_dump($_FILES['file']; // <<-- shows image data
$model['image_uploaded'] = $this->upload();
}
public function upload(){
$model = new UploadImage();
$model->load(Yii::$app->request->post());
$path = Yii::getAlias('@frontend') .'/web/upload/couriers/images/';
//return $path;
$model->file = UploadedFile::getInstance($model, 'file');
echo json_encode($model->file); # <<--- shoes nothing
$model->path = $path;
if($model->upload()){
return true;
}else{
return $model->getErrors();
}
}
,我得到的錯誤如下:
Please upload a file!
任何幫助將是最受歡迎!
檢查模型加載發佈數據? –
沒有發佈的數據沒有加載到$ model – goseo