🛡️
Security

Disabling PHP in the Upload Folder: Apache and Nginx Rules

25.11.2026
← All articles

If visitors can upload images, PDFs or CVs to your site, there is a folder on the server that stores files coming from outside. In WordPress it is wp-content/uploads; elsewhere it may be images, files or media. Its only job is to store files and serve them to browsers. If the server is also willing to execute a .php file sitting there, any small mistake in an upload form becomes a full site takeover. That is why disabling PHP in the upload folder is one of the cheapest and most effective protections you can add.

How attackers abuse the upload folder

The usual scenario is simple. Through an outdated plugin, a weak file check or a stolen admin password, the attacker drops a small PHP file onto the server, often hidden among images as image.php, cache.php or a random name. They then open that file's URL, the server runs it, and they now have a web shell that can read and modify files, connect to the database and send spam.

The key point is that uploading a file is not the damage; the damage starts when the file is executed. If PHP does not run in the upload folder at all, a planted file simply returns 403 or is shown as plain text. This is defence in depth: even if the first layer (file validation) fails, the second one stops the attack.

Apache: blocking with .htaccess

Most shared hosting, including ISPmanager-based servers, lets Apache read .htaccess files. Create a new .htaccess inside the upload folder with the rule below. It denies requests to PHP-like files regardless of how PHP is wired in (mod_php, PHP-FPM or CGI):

# wp-content/uploads/.htaccess
<FilesMatch "(?i)\.(php[0-9]?|phtml|phar|pht)(\.|$)">
    Require all denied
</FilesMatch>

# if mod_php is in use, switch the interpreter off too
<IfModule mod_php.c>
    php_flag engine off
</IfModule>
<IfModule mod_php7.c>
    php_flag engine off
</IfModule>

The (\.|$) part also catches double extensions such as shell.php.jpg, which some older server setups will happily run as PHP. The php_flag line is wrapped in IfModule on purpose: on a PHP-FPM server without mod_php, a bare php_flag would make the whole folder return 500 errors.

Nginx: a location block

Nginx ignores .htaccess, so on a VPS the rule goes into the server config. Place it before the general location ~ \.php$ block, because Nginx evaluates regex locations in order and uses the first match:

location ~* ^/wp-content/uploads/.*\.(php[0-9]?|phtml|phar|pht)$ {
    deny all;
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Then test and reload: sudo nginx -t && sudo systemctl reload nginx. The socket path depends on your PHP version, so copy it from your existing config.

Verifying that the rule works

Writing the rule is not enough; test it. Put a harmless test file in the folder, request it and check the status code. A 403 means you are protected; seeing the word "works" means the rule is not applied. Remove the file afterwards:

echo '<?php echo "works";' > wp-content/uploads/test.php
curl -I https://example.uz/wp-content/uploads/test.php
# Expected: HTTP/1.1 403 Forbidden
rm wp-content/uploads/test.php

Is there PHP in the folder already?

Before adding the block, scan the folder once. A PHP file among images is almost always a red flag. Some plugins add an empty index.php containing only a comment, but a file calling eval, base64_decode or system is almost certainly malicious:

find wp-content/uploads -type f -iname "*.ph*" -exec ls -la {} \;
grep -rlE "eval\(|base64_decode\(|system\(" wp-content/uploads --include="*.ph*"

If you find one, copy it somewhere safe before deleting it, since it helps establish when and how the breach happened. Then change every admin password and update plugins, or the attacker will return through the same hole.

Example: a clothing shop in Tashkent

A small Chilanzar clothing store runs WordPress with WooCommerce; customers send photos for sizing and the manager uploads product shots. One day the owner notices casino pages under the shop's domain in Google. Investigation finds a web shell named thumb.php in wp-content/uploads/2024/03, planted through an old gallery plugin. After removing it, the admin adds the .htaccess rule above. Two weeks later the logs show requests to a fresh PHP file in uploads, all ending in 403: the attacker re-planted the file but could not run it, and the plugin was updated in the meantime.

Details worth remembering

On sayt.uz hosting you can add this rule in a few minutes with the ISPmanager file manager, which opens from your cabinet. If you get stuck or find a suspicious file, open a support ticket.

Related articles

⚛️ RCE vulnerability in Next.js and React: how to check your project 🗝️ Passkeys: How Passwordless Sign-In Actually Works 🗂️ 644 and 755: Correct File Permissions for Websites 🐚 What is a webshell and how to find one on your server
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓