Solved: Use of $_FILES is Discouraged in Magento 2
The direct use of global variables such as $_FILES, $_GET, $_POST, can lead to security vulnerabilities and it is against the Magento coding standards. Thus, the direct usage of such global variables in Magento may throw an exception and show warnings.
In case you have tried using the $_FILES variable directly in your code, you may encounter the following warning: ‘Use of $_FILES is discouraged in Magento 2’.
1 2 |
45 | WARNING | The use of function filectime() is discouraged 46 | WARNING | The use of function filemtime() is discouraged |
That means you can not directly use the global variable $_FILES in your code and may require to seek for an alternate method to using it.
You may require to use these global variables at various points, in case you are developing any custom functionality in your Magento store such as Magento extension. Therefore, in this post, I have provided a complete solution on how you can use the $_FILES global variable in your code indirectly to prevent the error.
A solution to the Error – Use of $_FILES is discouraged in Magento 2:
Instead of directly using the global variable $_FILES in Magento, you can use the getFiles() to retrieve the files and prevent the error in Magento 2. You can use the following code to do that:
1 2 |
use Magento\Framework\App\Request\Http; $files = $this->httprequest->getFiles(); |
That’s it.
Any doubts? If so, do mention them in the Comments section below. I’d be happy to help you out.
Also, do share the post with the Magento Community via social media.
Thank you.
Prev
How to Add Quick View Popup in Magento 2
New Magento 2 Extensions, Updates & Services Launches [February 2022]
Next