我想使用yii2更新上传的文件。我有代码但没有任何反应。我不知道我的代码有什么问题。帮帮我们
这是我的表格
<?= $form->field($model, 'doc_for')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'doc_from')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'drawer_id')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'doc_name')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'doc_file')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-primary' : 'btn btn-primary']) ?>
</div>
模型
public function rules()
{
return [
[['reference_no', 'subject', 'doc_date', 'doc_for', 'drawer_id','doc_from','doc_name','doc_file'], 'required'],
[['reference_no'], 'integer'],
[['doc_date'], 'safe'],
[['subject', 'doc_for', 'drawer_id','doc_from','doc_name'], 'string', 'max' => 250],
[['doc_file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'docx, docs, doc',],
];
}
控制器创建
public function actionCreate()
{
$model = new Documents();
if ($model->load(Yii::$app->request->post()))
{
$project =$model->doc_name;
$model->upload_file= UploadedFile::getInstance($model,'doc_file');
$model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
$model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
$model->save();
Yii::$app->getSession()->setFlash('success','Data saved!');
return $this->redirect(['view','id'=> $model->reference_no]);
}
else {
return $this ->render('create', [
'model'=>$model,
]);
}
}
控制器更新
public function actionUpdate($id)
{
$model = new Documents();
$model = $this->findModel($id);
$current_image = $model->doc_file;
$project =$model->doc_name;
if ($model->load(Yii::$app->request->post())) {
$model->upload_file= UploadedFile::getInstance($model,'doc_file');
if(!empty($upload_file) && $upload_file->size !== 0) {
//print_R($image);die;
$model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
$model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
$model->upload_file = $current_image;
$model->save();
Yii::$app->getSession()->setFlash('success','Data updated!');
return $this->redirect(['view','id'=> $model->reference_no]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
我不知道什么是错的。有什么想法吗?
答案 0 :(得分:0)
请尝试以下代码
public function actionUpdate($id)
{
$model = $this->findModel($id);
$oldmodel = $this->findModel($id);
$oldimage = $model->image;
if ($model->load(Yii::$app->request->post()) ) {
if(isset($_FILES['image']['tmp_name']) && $_FILES['image']['tmp_name'] != null)
{
if($oldimage && file_exists(Yii::getAlias('@webroot')."/".$oldimage))
{
unlink(Yii::getAlias('@webroot')."/".$oldimage);
}
$image = $_FILES['image'];
$file = $_FILES;
$new_image['name'] = $_FILES['image']['name'];
$new_image['type'] = $_FILES['image']['type'];
$new_image['tmp_name'] = $_FILES['image']['tmp_name'];
$new_image['error'] = $_FILES['image']['error'];
$new_image['size'] = $_FILES['image']['size'];
$image = $new_image;
$model->image = Yii::$app->params['imagepath'].time().$file['image']['name'];
/**
* for uploding image as it is in folder
*/
move_uploaded_file($file['image']['tmp_name'],Yii::getAlias('@webroot')."/".$model->image);
}
else
$model->image = $oldmodel->image;
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
它将解决您的问题。
答案 1 :(得分:0)
好的,谢谢你的帮助。我知道了。现在它正常运作。这是代码。
actionUpdate Controller:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$current_image = $model->upload_file;
if ($model->load(Yii::$app->request->post())) {
$project =$model->doc_name;
$model->upload_file= UploadedFile::getInstance($model,'doc_file');
if(!empty($model->upload_file) && $model->upload_file->size !== 0) {
//print_R($image);die;
$model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
$model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
}
else
$model->upload_file = $current_image;
$model->save();
Yii::$app->getSession()->setFlash('success','Data updated!');
return $this->redirect(['view', 'id' => $model->reference_no]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}