Vinit Singh Vinit Singh - 2 months ago 21x
PHP Question

How to get Html::fileInput() value in yii2 Action?

I am using

<?= Html::fileInput() ?>
instead of
<?= $form->field($model, 'image_file')->fileInput() ?>
because i haven't passed any model to my form I am using Yii2 active form.

Now My question is that when i post this form how could i access this file in my action. I could see the file attributes using $_FILES variable. but is there any Yii method to access it.

I tried this also:

$documentObject = UploadedFile::getInstance(Yii::$app->request->post(), 'image_file');

but it is showing this Exception

Call to a member function formName() on array

Now how to access this file in action

Thanks in advance for any kind of help.


this should work (from here):

$documentObject = UploadedFile::getInstanceByName('image_file');

where image_file is your html input's name.