Reputation: 1
Yii2: I am updating a pdf file, but when doing the action I get this error. The path in the source code is created, just like the file is saved in the created path, and in the database. but after performing this process I get this error
Script PHP-Yii2
// Update PDF
$pathPdf = 'uploads/pdf/userSettings/';
if (!is_dir($pathPdf)) {
mkdir($pathPdf, 0777, true);
}
if(UploadedFile::getInstance($model, 'file_pdf')){
$model->file_pdf = UploadedFile::getInstance($model, 'file_pdf');
$file = $pathPdf . md5($model->company_name) . '.' . $model->file_pdf->extension;
$model->pdf_front_path = $file;
}
if (!$model->validate()) {
$errors = $model->errors;
$this->showErrorMessages($tab);
} else {
if ($model->save()) {
if (UploadedFile::getInstance($model, 'file_pdf')) {
$model->file_pdf->saveAs($file);
}
} else {
Yii::$app->session->setFlash('error', Yii::t('app', 'error_save'));
}
}
Could you help me, I have tried several things but nothing has worked for me
Upvotes: 0
Views: 1620
Reputation: 1
You can solve it simply by moving the line ''$model->file_pdf->saveAs($file);'' under '''$model->pdf_front_path = $file;''' for some strange reason Yii jumps that error with the routes, it is as if it did a double validation of this route, but on the second occasion it validates it no longer finds it.
the code stayed like this
// Update PDF
$pathPdf = 'uploads/pdf/userSettings/';
if (!is_dir($pathPdf)) {
mkdir($pathPdf, 0777, true);
}
if(UploadedFile::getInstance($model, 'file_pdf')){
$model->file_pdf = UploadedFile::getInstance($model, 'file_pdf');
$file = $pathPdf . md5($model->company_name) . '.' . $model->file_pdf->extension;
$model->pdf_front_path = $file;
$model->file_pdf->saveAs($file);
}
after that you can already perform the validations you want, but they are not from the route
Upvotes: 0