Wrong Permissions When Uploading Files on WordPress
Issue
When uploading images via the WordPress media library on Windows, the original image file permissions may be removed entirely. This results in broken thumbnails and original images, even though the resized images are viewable. The issue isn't specific to WordPress; it can also happen with basic PHP functions like:
move_uploaded_file($_FILES["image"]['tmp_name'], $target_file);
Solution
When PHP uploads a file, it first places the file in a temporary directory (usually C:\Windows\Temp) before moving it to the intended directory. The file inherits the permissions from the temporary directory. On Windows, when the file is moved, it retains the temporary directory's permissions, which can lead to access issues.
Fixing Permissions:
-
Grant Permissions to wp-content Folder:
- Give the IUSR account Read/Write/Modify permissions on your WordPress wp-content folder. This will allow uploads and enable WordPress and plugin updates.
-
Grant Permissions to Windows Temp Folder:
-
Grant the IIS_IUSRS group Read permissions on your C:\Windows\Temp folder. This ensures proper access for files during the upload process.
Important: The permissions for these folders are different:
- IUSR on wp-content
- IIS_IUSRS on C:\Windows\Temp
-
-
Custom Temp Directory:
- If you've modified your
php.inifile and set a custom temp directory, grant IIS_IUSRS group permissions on that directory instead.
- If you've modified your
Alternative Solution: Change Temp Directory in php.ini
You can avoid this issue by changing the temporary upload directory to a folder within your WordPress installation. Typically, this would be the wp-content/upgrade folder. Follow these steps:
- Find your
php.inifile. - Locate the
upload_tmp_dirsetting, and change it to thewp-content/upgradefolder. - Verify folder permissions to ensure they're set correctly.
Final Steps
After making these changes, your images should upload and display properly. For any previously uploaded images, you may need to adjust the file ownership to the web server's user (e.g., IUSR or similar). This should fix any broken images and thumbnails.