0

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.

1 Answer 1

3

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!

2
  • That is exactly what was missing. Thank you kindly!
    – Hurrpadurr
    Commented Sep 9, 2021 at 9:17
  • Most welcome buddy 🙂 Commented Sep 10, 2021 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.