Answer the question
In order to leave comments, you need to log in
Why is the file name not being written to the database?
Implemented adding a file. The file is saved in the project folder, but the name is not transferred to the database. I can’t understand why
the UploadCVForm model, where the file is saved to a folder and the file name is returned
public $file;
public function upload(UploadedFile $file)
{
$this->file = $file;
$filename = strtolower(md5(uniqid($file->baseName)). '.' .$file->extension);
$file->saveAs('uploads/' . $filename);
return $filename;
}
$model_file = new UploadCVForm();
if (\Yii::$app->request->isPost) {
$profile = $this->module->manager->findProfileById(\Yii::$app->user->identity->getId());
$file = UploadedFile::getInstance($model_file, 'file');
$profile->saveFile($model_file->upload($file));
if ($model_file->upload($file)) {
// file is uploaded successfully
return $this->refresh();
}
}
public function saveFile($filename)
{
$this->CV = $filename;
return $this->save(false);
}
<?php $form = \yii\widgets\ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model_file, 'file')->fileInput() ?>
<?= \yii\helpers\Html::submitButton(Yii::t('user', 'Добавить'), ['class' => 'btn btn-success']) ?><br>
<?php \yii\widgets\ActiveForm::end() ?>
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