WordPress Security ServicesMalware Removal
Hardening

How to Secure WordPress File Uploads and Block PHP in the Uploads Folder

The uploads folder is where WordPress stores your images and documents, and where I find more backdoors than anywhere else. Attackers love it because it's writable and full of files nobody looks at. A few settings make uploads far safer.

Sergey By Sergey, WordPress security specialist Updated 2 min read
In this guide
  1. 1. Block PHP execution in the uploads folder
  2. 2. Allow only the file types you need
  3. 3. Check what’s already in uploads
  4. 4. Protect private files
  5. 5. Limit who can upload

1. Block PHP execution in the uploads folder

The most important step. Even if an attacker manages to upload a PHP file, it won’t run.

Apache: create a file named .htaccess inside wp-content/uploads/ with:

<FilesMatch "\.(php|phtml|phar|php[0-9])$">
  Require all denied
</FilesMatch>

Nginx: ask your host to add a rule that denies .php files under /wp-content/uploads/.

Many security plugins (Solid Security, AIOS and others) can apply this setting for you. If your site is behind Cloudflare, a firewall rule can block these requests too. See Cloudflare firewall rules for WordPress.

2. Allow only the file types you need

WordPress already restricts uploads to common media types. Don’t widen that list unless you must, and be careful with plugins that add “allow any file type” options. Never allow .php, .phtml, .html or .js uploads from untrusted users.

3. Check what’s already in uploads

Find PHP files that shouldn’t be there:

find wp-content/uploads -type f \( -name "*.php" -o -name "*.phtml" -o -name "*.phar" \) -not -name "index.php"

Also look for files whose extension doesn’t match their content, such as a .jpg or .ico that’s actually code. See Is this file malware?

4. Protect private files

Files in wp-content/uploads are public by default: anyone with the link can open them. For private documents (invoices, member downloads, form uploads):

  • store them outside the web root, or use a plugin that serves them only to authorized users;
  • make sure directory listing is off (see directory listing);
  • never leave backups or database exports in uploads.

5. Limit who can upload

Only give upload rights to roles that need them, review users regularly, and turn on two-factor authentication. A taken-over editor account can upload anything an editor can.

From my cleanup work: in a large share of the cleanups I do, at least one backdoor sits somewhere in wp-content/uploads, often in an old year folder like uploads/2021/03/ with an innocent name. Blocking PHP in uploads wouldn’t have stopped the original hack, but it would have made that backdoor useless.

Sergey, WordPress Security Specialist
About the author
Sergey, WordPress Security Specialist

10 years in WordPress security and incident investigation, and more than 200 websites cleaned of malware. Top Rated on Upwork with a 100% Job Success score. More about me

Frequently Asked Questions

Will blocking PHP in uploads break my site?

Almost never. Uploads should only contain media and documents. A few plugins do store PHP files there; if something breaks after the change, add an exception for that plugin's folder.

Can someone upload malware through the media library?

WordPress only lets logged-in users with upload rights add files, and restricts file types. The risks come from vulnerable plugins that add their own upload features, and from accounts that have been taken over.

Should I allow SVG uploads?

Be careful. SVG files can contain scripts. Only allow them for trusted administrators, and use a plugin that sanitizes them.

Need a hand?

Let me clean your WordPress site for you

Send me your site URL and what you're seeing. I'll reply with what I think is going on and the next step.

$95 per infected site · 1-month guarantee · direct contact with the specialist

Get My Site Cleaned →