{"id":1977,"date":"2022-02-28T11:09:16","date_gmt":"2022-02-28T11:09:16","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/solved-use-of-files-is-discouraged-in-magento-2\/"},"modified":"2025-05-22T09:16:28","modified_gmt":"2025-05-22T03:46:28","slug":"solved-use-of-files-is-discouraged-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/solved-use-of-files-is-discouraged-in-magento-2\/","title":{"rendered":"Solved: Use of $_FILES is Discouraged in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you have tried using the $_FILES variable directly in your code, you may encounter the following warning:&nbsp;<strong>\u2018Use of $_FILES is discouraged in Magento 2\u2019<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> 45 | WARNING | The use of function filectime() is discouraged\n 46 | WARNING | The use of function filemtime() is discouraged<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That means you can not directly use the global variable&nbsp;<strong>$_FILES&nbsp;<\/strong>in your code and may require to seek for an alternate method to using it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento extension<\/a>. 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>A solution to the Error \u2013 Use of $_FILES is discouraged in Magento 2:<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of directly using the global variable&nbsp;<strong>$_FILES&nbsp;<\/strong>in Magento, you can use the&nbsp;<strong>getFiles()&nbsp;<\/strong>to retrieve the files and prevent the error in Magento 2. You can use the following code to do that:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Framework\\App\\Request\\Http;\n$files = $this->httprequest->getFiles();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post with the Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get the solution to the error &#8220;Use of $_FILES is Discouraged in Magento 2&#8221; and prevent the use of global variables.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1977","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1977"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1977\/revisions"}],"predecessor-version":[{"id":14158,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1977\/revisions\/14158"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}