How to Block PHP Execution in wp-content/uploads Folder
Step-by-step guide to blocking PHP execution in WordPress uploads folder. Prevent malware, webshells, and backdoors with this essential security measure.
Blocking PHP execution in wp-content/uploads is one of the most important WordPress security measures. This guide shows you multiple methods to implement this protection and verify it's working.
Why wp-content/uploads Needs Protection
The uploads folder is WordPress's designated location for media files—images, PDFs, videos, and documents. By design, it's writable by WordPress. Unfortunately, this makes it a prime target for attackers.
The Risk
- Attackers upload PHP webshells disguised as images
- Vulnerable plugins allow arbitrary file uploads
- Compromised admin accounts can upload malicious files
- Server misconfigurations allow PHP execution
The Solution
Configure your server to refuse executing PHP files in the uploads directory. Even if a PHP file gets uploaded, it cannot run.
Method 1: Using .htaccess (Apache/LiteSpeed)
Create or edit wp-content/uploads/.htaccess:
Basic Protection
# Disable PHP execution
<Files *.php>
deny from all
</Files>
Comprehensive Protection
# Disable all PHP extensions
<FilesMatch ".(php|phtml|php3|php4|php5|php7|php8|phps|phar)$">
Order Allow,Deny
Deny from all
</FilesMatch>
# Block double extensions
<FilesMatch ".php.">
Order Allow,Deny
Deny from all
</FilesMatch>
# Remove PHP handler for this directory
<IfModule mod_php.c>
php_flag engine off
</IfModule>
<IfModule mod_php7.c>
php_flag engine off
</IfModule>
<IfModule mod_php8.c>
php_flag engine off
</IfModule>
Method 2: Using nginx Configuration
Add to your nginx server block or site configuration:
# Block PHP in uploads
location ~* /wp-content/uploads/.*.php$ {
deny all;
}
# Alternative: return 403 for any PHP
location ~* /wp-content/uploads/.*.php$ {
return 403;
}
After editing, restart nginx:
sudo systemctl restart nginx
Method 3: Using WP Folder Shield (Recommended)
WP Folder Shield automates directory protection with one-click activation:
- Install and activate WP Folder Shield
- Navigate to WP Folder Shield > Dashboard
- Enable "Protect Uploads Directory"
- Verify protection status shows green
Advantages of WP Folder Shield
- Automatic restoration: If rules are removed, they're restored
- Verification: Confirms protection is actually working
- Multi-server support: Works on Apache, LiteSpeed, and nginx
- Conflict detection: Warns if plugins might be affected
- Multisite support: Handles network configurations
Verifying Protection is Working
Method 1: Create Test File
- Create a file named
test.phpwith content:<?php echo "Vulnerable!"; ?> - Upload it to wp-content/uploads via FTP
- Try to access:
yoursite.com/wp-content/uploads/test.php - If protected, you'll see 403 Forbidden or a download prompt
- Delete the test file immediately
Method 2: WP Folder Shield Verification
WP Folder Shield includes a built-in verification feature that tests protection without creating test files accessible to attackers.
Troubleshooting Common Issues
Plugin Compatibility
Some plugins incorrectly store PHP files in uploads. If a plugin breaks after enabling protection:
- Contact the plugin developer—this is a security issue
- Find an alternative plugin that follows best practices
- As a last resort, whitelist specific subdirectories
.htaccess Not Working
If .htaccess rules don't take effect:
- Verify Apache AllowOverride is set to All
- Check file permissions (should be 644)
- Restart Apache/LiteSpeed
- Check for syntax errors in .htaccess
Managed Hosting Restrictions
Some hosts don't allow .htaccess modifications. Options:
- Contact host support to enable protection at server level
- Request they add rules to main server configuration
- Consider switching to a more security-friendly host
Additional Protections for Uploads
PHP blocking is essential but not sufficient alone:
- Upload scanning: Check files before they reach the server
- Extension filtering: Only allow safe file types
- Content verification: Ensure files match their extensions
- Malware scanning: Regularly scan uploads directory
WP Folder Shield provides all these protections. Get WP Folder Shield for comprehensive uploads directory security.
Written by Marcus Johnson
WP Folder Shield Team