我使用kartik fileUpload扩展名在yii2中上传文件,一旦我选择一个文件并提交,我就得到
在非对象上调用成员函数savea ()
我已经查看了其他关于这个问题的帖子,但没有帮助,
My View code..
<?php echo $form->field($documents, 'sars_certificate')->label(false)->
widget(FileInput::classname(), [
'pluginOptions' => [
'showCaption' => true,'showRemove' => true,'showClose' => true,
'showPreview' => true,'uploadAsync' => true,
'showUpload' => false,'maxFileSize'=> 2000,'autoReplace'=> true,
'placeholder' => 'Select a File...',],]); ?>
My model code..
[['sars_certificate'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,pdf,jpeg']
和My controller code..
public function actionCreate()
{
$model = new Projects();
$documents = new ProjectDocuments();
$borrower_id = Yii::$app->user->identity->id;
$code = $model->accessCode(10);
if ($model->load(Yii::$app->request->post()))
{
//All $model related things will bw done
//In this im doing file upload
$documents->borrower_id = $borrower_id;
$documents->project_id = $code;
$documents->save(false);
$documents->sars_certificate = UploadedFile::getInstances($documents,'sars_certificate');
$documents->sars_certificate->saveAs('user/purchase_order/' . $documents->sars_certificate->baseName . '.' . $documents->sars_certificate->extension);
$documents->sars_certificate = $documents->sars_certificate;
$documents->save(false);
return $this->redirect(['index']);
}
我给出了if($documents->validate()){ **** }
。但它本身不符合条件,所以我把它去掉了。现在它说上面的错误…
请大家帮帮我,我在这上面浪费了很多时间。
1)为避免重复图像,请在图像名称中添加time()
2) 删除$documents->save(false);
UploadInstance
。
public function actionCreate() {
$model = new Projects();
$documents = new ProjectDocuments();
$borrower_id = Yii::$app->user->identity->id;
$code = $model->accessCode(10);
if ($model->load(Yii::$app->request->post())) {
//All $model related things will bw done
//In this im doing file upload
$documents->borrower_id = $borrower_id;
$documents->project_id = $code;
if(UploadedFile::getInstance($documents, 'sars_certificate')){
$image = UploadedFile::getInstance($documents, 'sars_certificate');
$imageName = time().$image->name;
$path = "user/purchase_order/".$imageName;
if($image->saveAs($path)){
$documents->sars_certificate = $imageName;
}
}
$documents->save(false);
return $this->redirect(['index']);
}
}
public function actionCreate() {
$model = new Projects();
$documents = new ProjectDocuments();
$borrower_id = Yii::$app->user->identity->id;
$code = $model->accessCode(10);
if ($model->load(Yii::$app->request->post())) {
//All $model related things will bw done
//In this im doing file upload
$documents->borrower_id = $borrower_id;
$documents->project_id = $code;
if(UploadedFile::getInstance($documents, 'sars_certificate')){
$image = UploadedFile::getInstance($documents, 'sars_certificate');
if($image){
$imageName = time().$image->name;
$path = "user/purchase_order/".$imageName;
if($image->saveAs($path)){
$documents->sars_certificate = $imageName;
}
}
}
$documents->save(false);
return $this->redirect(['index']);
}
}