Problems getting UploadedFile instance in Yii2
I tried to upload a file to the server using a class UploadedFile
, but I cannot get an instance. In my model:
public $arch;
public function rules() {
return [[['arch'], 'file']];
}
Before $model->arch = file_xxxx.jpg
Controller:
$model->arch = UploadedFile::getInstance($model, 'arch');
Thereafter $model->arch is NULL
View:
$form = ActiveForm::begin(
['id' => 'contact-form'],
['options' => ['enctype' => 'multipart/form-data']]
);
print $form->field($model, 'arch')->fileInput()->label(false);
+3
source to share
3 answers
You can try to get the file like this:
// View
<?= $form->field($model, 'arch')->fileInput(); ?>
// Controller
$model->arch = UploadedFile::getInstanceByName('arch');
getInstanceByName()
- returns the loaded file according to the specified file input name.
Complete the yii2 File Upload Guide .
+5
source to share