Belongs in uploads
Written by WordPress and plugins
- Media
- Images, PDFs, audio, video
- Generated assets
- CSS, JSON, fonts from builders and caches
- Guard files
- Blank index.php or index.html, deny-all .htaccess
- Runs PHP?
- Never needs to
Guide · PHP in the uploads folder
The uploads folder is for media. WordPress writes images and documents into it, and nothing in WordPress core needs to execute PHP from it. So when a scanner, your host or your own look through the file manager turns up a .php file in wp-content/uploads — in a year/month folder next to your photos, under a name like cache.php or wp-tmp.php, or disguised as photo.php.jpg — the default assumption is a web shell or a loader that someone placed there to run code on your server. This guide covers the rare legitimate exceptions, how a PHP file gets into uploads, the read-only checks that find every one including files hidden inside images, and the removal order that ends with the folder unable to run PHP at all.
Published Last reviewed
What belongs there
wp-content/uploads is where WordPress stores what users upload through the media library, sorted by default into year and month folders. Plugins also write into it: page builders put generated CSS there, form plugins keep submitted attachments, backup and cache plugins keep their files, and WooCommerce keeps downloadable products in a protected subfolder. Everything WordPress itself writes there is data to be served or read, not code to be run. WordPress also refuses PHP through the media library: its upload check only accepts an allowed list of file types, and PHP is not on it.
The legitimate exceptions are narrow. Some plugins drop a blank index.php — often just a comment such as “Silence is golden” — into their uploads subfolders so the web server never shows a directory listing, and some add a .htaccess that denies access to the folder. A handful of older plugins have kept small PHP files in uploads for their own reasons. In every case the file is short, readable and attributable to a plugin you can name.
Treat names as a clue, never a verdict. Attackers use names that look like WordPress or cache files, and a legitimate plugin can use a name that looks odd. Open the file and read it. A blank index file is a few bytes of comment; a web shell has a password form, a file manager, a command box, or a single line that decodes and runs something it receives.
Figure 1 · Media folder or code folder
WordPress writes media into uploads. It never needs to run PHP from there.
Written by WordPress and plugins
Written by an attacker
How it got there
The file is the result, not the cause. Finding how it was written is what stops the next one.
Form, file-manager, gallery, import and theme-option plugins that save uploaded files without properly checking type and content are the most common way in. Vulnerabilities of this kind are published regularly for WordPress plugins, and automated scanners try them within days.
Files named photo.php.jpg, shell.phtml, file.phar, x.php5 or x.php7 slip past checks that only look at the last extension or only block .php. Depending on how the server maps extensions to PHP, some of these run as code.
A web shell can begin with a real image header such as GIF89a and still contain PHP. It passes a quick type check and only becomes dangerous when something includes it or the server is told to run it.
An attacker who can write files often adds a .htaccess to the folder with AddHandler, AddType or SetHandler lines, so that .jpg, .ico or .txt files are run as PHP. Your own blocking rule in the same folder can be deleted the same way.
A compromised administrator, SFTP or hosting-panel account can write anywhere. Uploads is chosen because it is writable by the web server and full of files nobody reviews.
A loader elsewhere — in a plugin, a theme, the database or a scheduled task — may write the uploads file again after you delete it.
Find every one
Run these from the WordPress root over SSH. None of them change anything. If uploads is large, the content search takes a while; let it finish rather than stopping at the first hit.
Every file whose name contains a PHP-style extension, including double extensions and upper-case variants:
find wp-content/uploads -type f \( -iname '*.php*' -o -iname '*.phtml' -o -iname '*.phar' -o -iname '*.pht' \) -ls
Blank index.php files from plugins will appear here. Read each one; anything longer than a comment needs explaining.
Files of any type that contain a PHP opening tag, which catches code hidden in images and text files:
grep -rlaE '<\?(php|=)' wp-content/uploads
The -a flag searches binary files too, so an image with PHP appended is found. A real photo very rarely contains this string by chance; open any match to confirm.
Every .htaccess and .user.ini under uploads, and the lines that change how files are run:
find wp-content/uploads \( -name '.htaccess' -o -name '.user.ini' \) -ls
find wp-content/uploads -name '.htaccess' -exec grep -HniE 'AddHandler|AddType|SetHandler|ForceType|php_value|php_flag|auto_prepend' {} +
A deny-all .htaccess from a plugin is normal. A rule that maps .jpg, .ico, .txt or any other extension to PHP is not.
Files whose content or metadata changed after a date you choose, newest last:
find wp-content/uploads -type f -newerct '<date>' -printf '%C+ %u %s %p\n' | sort
This uses the change time (ctime), which a site user cannot set back with touch, unlike the modification time. Media uploaded in the same window will appear too; it is the non-media files that matter.
Requests in the access logs for PHP under uploads that the server answered with 200:
zgrep -hE '"(GET|POST) /wp-content/uploads/[^" ]*\.(php[0-9]?|phtml|phar|pht)[^" ]* HTTP/[0-9.]+" 200' <access-log>*
Log locations vary by host; on cPanel accounts they are often under ~/logs. A POST answered with 200 means the file ran. The source addresses and times date the first use and show whether other files were requested the same way.
Remove it
Deleting the file is the last step of the part that matters, not the first. The order keeps the evidence and closes the way in before anything is written again.
Copy every suspect file, with its timestamps, outside the web root, and save the access logs for the incident window. Hosts rotate logs, sometimes within days.
Match the first request to the shell against the logs: what was requested just before it was written, by which address, and through which plugin or login. Update or remove the plugin that accepted the file; if it has no fixed version, remove it.
Delete the PHP files, disguised images and any .htaccess or .user.ini that changed how files run. Keep the blank index files and deny-all rules that a plugin you can name put there.
A shell is a tool for doing something else. Check for new administrators, changed plugins and themes, drop-ins, scheduled tasks, database injections and other accounts on the same hosting account, then rotate WordPress, hosting, SFTP and database passwords and the keys in wp-config.php.
Stop the folder running PHP even if a file is written there again. On Apache, put this .htaccess in wp-content/uploads:
# wp-content/uploads/.htaccess — stop PHP running from the uploads folder
<FilesMatch "(?i)\.(php[0-9]?|phtml|phar|pht|phps)(\.|$)">
Require all denied
</FilesMatch>
The (\.|$) ending also catches double extensions such as photo.php.jpg. LiteSpeed honours .htaccess in the same way. The rule only holds while nobody can overwrite the file, which is why the entry point comes first.
On nginx, which ignores .htaccess, the rule goes in the server configuration, where the site user cannot remove it:
# In the server block, above the location that passes .php to PHP-FPM
location ~* ^/wp-content/uploads/.*\.(php[0-9]?|phtml|phar|pht|phps)(\.|$) {
deny all;
}
nginx checks regular-expression locations in order, so this must come before the block that passes .php files to PHP-FPM. On managed hosting, ask the host; some already block PHP in uploads for every site.
Re-run the extension and content searches after a few days of normal traffic, and request one of the old shell URLs: it should return 403 or 404. A file that reappears means something outside uploads is still writing it.
Why it matters
A web shell is a remote control. While it was reachable, whoever held it could read wp-config.php and the database password, create administrators, install plugins, change files anywhere the web server could write, and send mail from the server. Deleting the shell ends that access; it does not undo what was done with it.
That is why the shell is worth keeping as evidence. Its timestamps and the log entries that reached it bound the window to investigate, and its code often shows which plugin flaw or upload form was used. A cleanup that starts from that window finds the other changes; one that starts by deleting the file loses the trail.
Common questions
No, but nearly every one with real code in it is. Some plugins add blank index.php files containing only a comment, to stop directory listings. Read the file: a few bytes of comment is a guard file; anything that takes input, decodes strings or writes files is not.
Yes. A file can start with a real image header and still contain PHP. On its own it usually does not run, but a .htaccess rule mapping .jpg to PHP, or another file that includes it, makes it execute. Search uploads for the PHP opening tag, not just for .php names.
Very rarely. WordPress does not run PHP from uploads, and the few plugins that ever did have mostly moved away from it. Test the site after adding the rule; if a plugin breaks, ask its developer why it needs to run code from the uploads folder.
Either the way in is still open — usually a vulnerable plugin that accepts uploads — or a loader elsewhere on the site writes it again. Check the access logs for the request that created the new file, and the plugins, themes, database and scheduled tasks for a loader.
Not necessarily. Signature scanners miss new or heavily encoded shells, and some skip large media folders. A PHP file in uploads deserves reading whatever a scanner says about it.