DEV Community

Calin V.
Calin V.

Posted on Originally published at wpghost.com

Checking a WordPress uploads folder for PHP from the shell

An upload becomes an attack when the server runs it. On WordPress that file lands in wp-content/uploads/, a folder that should only ever serve images, PDFs and other data.

Patchstack's 2026 report counted 11,334 new vulnerabilities in the WordPress ecosystem in 2025. Plugins held 91% of them, and many plugins that take files from visitors ship their own upload code.

So the checks below assume one of those plugins will let a script through at some point.

1. Look for PHP that is already there

Run this from the WordPress root:

find wp-content/uploads -type f \
  \( -iname '*.php' -o -iname '*.phtml' -o -iname '*.php5' -o -iname '*.phar' \) \
  -printf '%TY-%Tm-%Td %p\n' | sort
Enter fullscreen mode Exit fullscreen mode

A clean site prints nothing. Some plugins drop an index.php with only a comment in it to stop directory listings, so open each hit before you delete anything.

Files that pass the extension test can still carry code:

grep -rl --include='*.jpg' --include='*.png' --include='*.gif' '<?php' wp-content/uploads
Enter fullscreen mode Exit fullscreen mode

Double extensions are worth a separate pass, because some old Apache setups run any file with .php anywhere in the name:

find wp-content/uploads -type f -iname '*.php.*'
Enter fullscreen mode Exit fullscreen mode

2. Block PHP in the folder

On Apache 2.4 or LiteSpeed, put this in wp-content/uploads/.htaccess:

<FilesMatch "\.(php|phtml|php5|phar)$">
Require all denied
</FilesMatch>
Enter fullscreen mode Exit fullscreen mode

On nginx, add this inside the server block:

location ~* ^/wp-content/uploads/.*\.(php|phtml|php5|phar)$ {
    deny all;
}
Enter fullscreen mode Exit fullscreen mode

Order matters here. nginx uses the first regex location that matches, so this block has to sit above your generic location ~ \.php$ block. If it sits below, PHP-FPM gets the request first and the deny never runs.

sudo nginx -t && sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

3. Prove it with one request

Drop a harmless test file in and request it:

echo '<?php echo "ran";' > wp-content/uploads/test.php
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/wp-content/uploads/test.php
rm wp-content/uploads/test.php
Enter fullscreen mode Exit fullscreen mode

You want 403. A 200 means PHP still runs in that folder, and the body will say ran.

4. Narrow the allowed types

Core already filters types through upload_mimes. A site that only needs photos and PDFs can say so in a small mu-plugin:

add_filter( 'upload_mimes', function ( $mimes ) {
    return array(
        'jpg|jpeg' => 'image/jpeg',
        'png'      => 'image/png',
        'pdf'      => 'application/pdf',
    );
} );
Enter fullscreen mode Exit fullscreen mode

SVG is left out on purpose. An SVG file can carry script, so only trusted users should upload one.

5. Change the path scanners look for

The deny rule stops the file from running. The attacker still needs its URL, and /wp-content/uploads/ is the first place any scanner looks.

WP Ghost can change the uploads path, and the default address then returns a 404. It does not scan files for malware, so keep step 1 on a schedule.

The full write-up, with the upload sources table and an FAQ for site owners, is on wpghost.com.

Top comments (0)