Uploaded image not showing in frontend - Magento
The problem
Today one of my customers contacted me with an interesting issue. Any images she uploaded with the wysiwyg editor were not displayed in the frontend. Going directly to the image URL resulted in a 404 error. In other words, she could upload files and images in admin, but they were not visible in the frontend.
The cause
A direct request to an existing file on the server resulting in a 404 is usually caused by incorrect file permissions. A quick look at the server showed that the permissions for newly uploaded files are set to 0640, whereas before installing security patch SUPEE-7405 they were set to 0777. The code responsible for this can be found in lib/Varien/File/Uploader.php where
chmod($destinationFile, 0777);
was changed to
chmod($destinationFile, 0640);
The solution
Obviously, not setting the file permissions for newly uploaded files to 0777 is a good thing. However, if you are using shared hosting where the webuser is not necessarily the owner of the file, setting stricter permissions can cause newly uploaded images to not be displayed. An easy, temporary fix is to change the file permissions or the owner of the newly uploaded file manually. To fix this issue long-term, there are two possible solutions:
- Properly configure your file owner (recommended)
- Hacking Uploader.php to set less secure file permissions (not recommended)
For the first (recommended) solution, you can read more on how to do this here or contact your hosting provider. For the second solution, copy
/lib/Varien/File/Uploader.php
to
/app/code/local/Varien/File/Uploader.php
then go to line 219 and update
chmod($destinationFile, 0640);
to the permissions you require. In my case 0644 was sufficient, so I updated line 219 to
chmod($destinationFile, 0644);
Before doing this, however, please make sure you understand the risks.