Guide · PHP in the uploads folder

PHP file in wp-content/uploads: is it malware, and how to remove it

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.

Symptom
A .php, .phtml or .phar file in wp-content/uploads, or an image that contains PHP code
What it usually is
A web shell, an uploader or a loader that runs whatever the attacker sends
Usual way in
A plugin that accepts uploads without checking them, or stolen access
Lasting fix
Close the entry point, then make uploads unable to run PHP

What belongs there

Why WordPress never needs PHP in the uploads folder

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.

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

Does not belong

Written by an attacker

PHP files
wp-tmp.php, cache.php, random names
Double extensions
photo.php.jpg, file.phtml, x.phar
Disguised code
A .jpg or .ico that starts with <?php
Handler rules
.htaccess adding AddHandler or SetHandler
A few plugins add blank index.php files or their own .htaccess to uploads subfolders. Read any file before deciding; never trust or condemn a file by its name alone. Illustrative reconstruction.

How it got there

How a PHP file ends up in uploads

The file is the result, not the cause. Finding how it was written is what stops the next one.

A plugin that accepted the upload

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.

Disguised extensions

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.

Code inside an image

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.

A rule that makes it run

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.

Access that was already stolen

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.

Another backdoor dropping it

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

Read-only checks for PHP in wp-content/uploads

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.

  1. 01

    Find files with PHP extensions, any case

    Every file whose name contains a PHP-style extension, including double extensions and upper-case variants:

    shell
    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.

  2. 02

    Find PHP code regardless of the extension

    Files of any type that contain a PHP opening tag, which catches code hidden in images and text files:

    shell
    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.

  3. 03

    Look for handler rules and PHP settings in the folder

    Every .htaccess and .user.ini under uploads, and the lines that change how files are run:

    shell
    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.

  4. 04

    List what changed in the incident window

    Files whose content or metadata changed after a date you choose, newest last:

    shell
    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.

  5. 05

    Check whether anything requested them

    Requests in the access logs for PHP under uploads that the server answered with 200:

    shell
    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

The removal order that keeps the folder clean

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.

  1. 01

    Preserve the files and the logs

    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.

  2. 02

    Find the entry point

    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.

  3. 03

    Remove the files and the rules

    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.

  4. 04

    Look beyond uploads

    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.

  5. 05

    Block PHP in uploads

    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 (Apache 2.4)
    # 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.

  6. 06

    Or block it in the nginx configuration

    On nginx, which ignores .htaccess, the rule goes in the server configuration, where the site user cannot remove it:

    nginx server block
    # 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.

  7. 07

    Verify over days

    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 shell in uploads means the site was entered, not just scanned

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

Common questions about PHP files in uploads

Is every PHP file in wp-content/uploads malware?

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.

Can a .jpg file be a web shell?

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.

Will blocking PHP in uploads break my site?

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.

I deleted the file and a new one appeared. Why?

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.

My security plugin did not flag the file. Is it safe?

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.