Answer the question
In order to leave comments, you need to log in
How to save a file after pressing save?
I am using demos.krajee.com/widget-details/fileinput widget. My problem is that when you click download everything, the file is normally loaded to the local computer and to the database, but if I transfer the save to the database after clicking on save, I can’t get the file format.
Here is the code in the model
public function upload($id)
{
if($this->validate()){
$this->file->saveAs('attachment/'.$id.'.'.$this->file->extension);
return true;
} else {return false;}
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if (Yii::$app->request->isPost){
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->upload($id)){
//$model->img = $id.'.'.$model->file->extension; Вот сюда нормально сохраняет в базу данных
if(!$model->save()){
print_r($model->getErrors());
} else {
$model->save();
}
return '{}';
}
}
}
public function actionZakazedit($id){
$models = $this->findModel($id);
if($models->load(Yii::$app->request->post()) && $models->validate()){
$model->file = UploadedFile::getInstance($model, 'file'); // если сюда перекинуть данный код, то я не получаю формат файла. Потому что логика должна быть, если не сохраняет то должно же попасть в бд или я не прав?
if (!$models->save()){
print_r($models->getErrors());
} else {
$models->save();
}
return $this->redirect(['admin', '#' => $models->id_zakaz]);
} else {
return $this->renderAjax('_zakazedit', ['models' => $models]);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question