Reputation: 109
I tried to upload a file to a server using UploadedFile
class, but I can't 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');
After this $model->arch is NULL
View:
$form = ActiveForm::begin(
['id' => 'contact-form'],
['options' => ['enctype' => 'multipart/form-data']]
);
print $form->field($model, 'arch')->fileInput()->label(false);
Upvotes: 3
Views: 12445
Reputation: 431
Check the upload error code. If the code number is 6, it is UPLOAD_ERR_NO_TMP_DIR meaning "Missing a temporary folder". Solution maybe ..:
Upvotes: 0
Reputation: 6560
Just hit that issue and it was my upload size in PHP ini file. It was 2MB and file was 2MB. I extended it to 200MB and all worked!
Upvotes: 0
Reputation: 1
I have encountered a similar problem and found that missing "s",
$names = UploadedFile::getInstances($model, 'filename');
and it works.
Upvotes: 0
Reputation: 16226
You can try to get a file as follows:
// View
<?= $form->field($model, 'arch')->fileInput(); ?>
// Controller
$model->arch = UploadedFile::getInstanceByName('arch');
getInstanceByName()
- returns an uploaded file according to the given file input name.
Complete yii2 Uploading Files guide.
Upvotes: 5
Reputation: 31
In my case, it happened because I forgot to add
'options' => ['enctype' => 'multipart/form-data']
to ActiveForm options.
Upvotes: 3