Magento2 – Can’t Get File in Post-Controller from Input in Phtml

file uploadphtml

I have a file-input field in my pthml and would like to use it to upload picture files.

[...]
                <div class="field file">
                    <label class="label" for="email"><span><?= $escaper->escapeHtmlAttr(__('Attach a picture')) ?></span></label>
                    <div class="control">
                        <input name="pictureUpload"
                               id="pictureUpload"
                               type="file"
                               class="input-file"
                               accept="image/*"
                               value="">
                    </div>
                </div>
[...]

My Post-Controller works with all other inputs but not with the file.
I tried getting it via $this->getRequest()->getFiles(); and checked $_FILES but it remains empty.
Is there another component involved that I'm missing?

My Controller extends \Magento\Framework\App\Action\Action and implements Magento\Framework\App\Action\HttpPostActionInterface.

Best Answer

Please check with below example your form all attributes is same like this.

<form id="contact-form" class="form contact" action="Your action" method="post" enctype="multipart/form-data">
    ............
    ............
</form>

This Form attribute enctype="multipart/form-data" is needed when you use file input in your phtml file.

In controller you can try below code for get input data.

$files = $this->getRequest()->getFiles();

Hope it will work for you!

Related Topic