In Magento admin panel while uploading an image for any product from “Image” tab you might get an error like $_FILES array is empty
Well, there are many issues which could cause this error, like your server’s temp directory is full or write protected etc.
But recently I found another issue which causes this error in one of my projects.
Problem :
We had to remove SID from the google indexed URL as this was causing a duplicate content issue for the same URL in search engines. So to solve this I added following code in .htaccess (Apache server configuration) file.
RewriteCond %{QUERY_STRING} "SID=" [NC] RewriteRule (.*) /$1? [R=301,L]
It means if any URL adds SID=xxxxxxxxx then this parameter will be removed and redirect to the same URL.
Now the problem occurs when someone is trying to upload image for any product from the admin panel, Magento system sends a POST request with SID parameter to the following URL,
https://example-domain.com/admin/catalog_product_gallery/upload/key/xxxxxxxxx/?SID=xxxxxxxxx
In the above image, you can see (in chrome browser with inspect element on network tab), sending request to catalog_product_gallery/upload/key/xxxxxxxxx/?SID=xxxxxxxxx URL in POST method with SID parameter.
But my rule in the .htaccess file is redirecting this URL without SID parameter, so the file upload is not completed and throws $_FILES array is empty error.
Solution :
Wondering what might be the solution for it, yes need to do something in the .htaccess file, you can do as follows
#RewriteCond %{QUERY_STRING} "SID=" [NC] #RewriteRule (.*) /$1? [R=301,L]
simply turn off the SID redirect code in the .htaccess file, but with this, the SID URL duplicate content issue will get back. So do this
RewriteCond %{REQUEST_METHOD} GET [NC] RewriteCond %{QUERY_STRING} "SID=" [NC] RewriteRule (.*) /$1? [R=301,L]
now the website is checking if there is any URL with SID parameter and GET method to redirect, but not with POST method. As the SID parameter with POST method has some role in the admin panel for certain functionality.
Hope this help you guys, happy coding 🙂 and I look forward to receiving your comments!