WordPress File Upload Security: Block PHP in Uploads
WordPress file upload security is the check most site owners never run, and it is the one that decides whether a single bad upload becomes a full site takeover. Every WordPress site has a folder that accepts files from the outside world: wp-content/uploads. Your visitors never see it directly, but every image in your media library lives there, every form attachment lands there, every avatar, PDF, and user-submitted file ends up in that one directory. It is the only folder on a normal WordPress site that is supposed to receive files from users, and it is also the only folder where an attacker can plant a file and then ask the server to run it. If the server treats an uploaded file as code, the attacker does not need your password. They upload a small PHP shell, open its URL in a browser, and they are inside your site with the privileges of the web server: reading wp-config.php, creating admin users, defacing pages, sending spam, and using your server to attack the next site.
This guide fixes that at the layer where it matters. You will learn how WordPress handles uploads and where the checks actually sit, the five ways an upload turns into a shell, and then the hardening itself, in order of impact: first, block PHP execution inside wp-content/uploads on Apache and on Nginx, so even a shell that slips through becomes inert data; second, restrict file types to a strict allow-list in WordPress, so dangerous extensions never reach the disk; third, make uploaded files boring by renaming them and stripping executable names; fourth, harden the rest of the upload path, from file permissions to the constants that stop dangerous features; and fifth, verify the whole setup with tests you can run in five minutes, plus a cleanup plan for the day you find a shell that is already there. Every code example is complete and copy-paste ready. Nothing here needs a paid plugin, and the server-level fix works even if a plugin or theme has a file-upload vulnerability you do not know about yet.
The stakes are not theoretical. File-upload flaws are one of the most common ways WordPress sites are compromised, and they keep appearing in plugin and theme vulnerabilities year after year. The November 2026 PHP security releases fixed critical flaws that could turn a file upload into remote code execution, and the plugin-vulnerability audit in this series showed how often the vulnerable code is a form, a media handler, or an import feature that accepts a file. The upload folder is where those flaws land. Hardening it is the cheapest insurance on a WordPress site: one server rule, one filter, and a test, and the most common attack path on the internet stops working against your site.
Quick Takeaways
- WordPress file upload security starts with one rule: no PHP, or any other script, may ever execute inside
wp-content/uploads. Put that rule at the web-server layer, not in a plugin, so it holds even when WordPress itself is compromised. - On Apache, create
wp-content/uploads/.htaccesswith aFilesMatchblock that denies every script extension, case-insensitively. On Nginx, add alocationblock that denies any.phprequest under uploads or files. Test both with a real uploaded test file. - WordPress already checks uploads three ways: the extension must be on the allowed list, the file contents must match the claimed type, and the uploader must hold the
unfiltered_uploadcapability for anything outside the list. Keep it that way. Never defineALLOW_UNFILTERED_UPLOADS, and never add dangerous types through theupload_mimesfilter. - SVG files are blocked by default for a reason: an SVG is an XML document that can carry scripts, event handlers, and
javascript:links. If you need SVG uploads, sanitize every file on upload and restrict them to trusted roles. Never enable them with an unsanitized one-line snippet. - Restrict who can upload at all. Only roles that genuinely need the
upload_filescapability should hold it, and on sites with open registration, treat every upload as untrusted input from a stranger. - Rename uploaded files on arrival, or at least strip executable extensions from the name. A shell named
photo.phpis dangerous; a file renamed to a random name with a safe extension is inert. - Permissions are part of upload security: files
644, directories755,wp-config.php600to640, and never777anywhere. A world-writable uploads folder lets any local process drop files into it. - Test after hardening: upload a harmless
.phptest file, request it with curl, and confirm a403. Test.phtml,.PHPin capitals, and a double extension too. A rule you never tested is a rule you do not have. - If you find a shell, do not just delete it. Take the site offline, restore from a clean backup, rotate every password and salt, and audit plugins, themes, and users, because a shell is almost never the only thing the attacker left behind.
Why Your Uploads Folder Is the Attacker’s Front Door
Every WordPress site has the same shape from an attacker’s point of view: a public front door, the login page, that everyone watches, and a side door, the uploads folder, that almost nobody locks. The login page has rate limiting, two-factor options, and lockout plugins. The uploads folder has none of that. It is a directory whose entire job is to accept files from outside and serve them back over HTTP, and by default the web server will happily execute any PHP file it finds there, because executing PHP files is what a PHP server does. The mismatch is the vulnerability: a folder designed for untrusted input, served by a server configured to run code.
Think about what lands in wp-content/uploads on a normal site. The media library is the obvious one, but it is far from the only path. Contact forms with file attachments, job application forms, support ticket uploads, membership avatars, WooCommerce product files, import tools, front-end post submission, and any plugin that accepts a file all write into the same directory, often into year and month subfolders, sometimes into plugin-specific subfolders. Each of those features is a separate upload handler, written by a different developer, with its own idea of which file types are safe. When one of them gets the check wrong, or when a vulnerability is found in one of them later, the file lands in uploads, and if the server will execute it, the attacker owns the site. That is why upload vulnerabilities keep appearing in WordPress security news: the vulnerable feature changes every time, but the landing zone is always the same folder.
The damage from an executed upload is total, because a PHP shell runs with the privileges of the web server user. From there the attacker can read wp-config.php and take the database credentials, dump the user table and its password hashes, create a new administrator account, install a malicious plugin, replace theme files, redirect visitors, send spam, host phishing pages, and use the server as a platform for the next attack. The shell itself is usually tiny: a few lines that take a command from a request parameter and run it. Some shells are a single line. The attacker does not need to upload a large file or a clever exploit. They need one small file that the server agrees to run.
The fix has two halves, and both matter. The first half is to stop dangerous files from reaching the disk: strict file-type checks, role limits, and sanitizing. The second half is to make sure that even if a dangerous file does reach the disk, it can never run: block script execution at the web-server layer. The second half is the more important one, because it does not depend on every plugin author getting validation right, and it does not depend on WordPress core never having a bypass. It is a server rule that says, in this directory, files are data, never code. This guide does the server rule first, because it is the highest-impact step, and then the WordPress-level controls that keep bad files out in the first place.
How WordPress Handles Uploads, and Where the Checks Sit
Before you harden anything, it helps to know exactly what WordPress does when a file arrives, because the checks are better than most site owners realise, and the gaps are specific. Every upload through the media library passes through one function, wp_check_filetype_and_ext(), and that function runs three checks in a fixed order.
The first check is the extension. WordPress matches the end of the file name against its allowed list, the list returned by the upload_mimes filter, which starts from wp_get_mime_types() in core. If the extension is not on the list, there is no type, and the upload is refused. The default list covers the ordinary media types: images, audio, video, documents, and a few others. It does not include php, phtml, exe, swf, htm, html, or js. In fact core explicitly unsets swf and exe outright, and it unsets htm, html, and js for anyone who does not hold the unfiltered_html capability. SVG is not in the list at all, which is a deliberate choice, covered in the file-types section below.
The second check is the real content. If the PHP fileinfo extension is loaded, WordPress asks it what the file actually contains and compares that answer against the claimed extension. A text file renamed to .jpg fails this check, because fileinfo reports text while the extension claims an image. This is the check that catches the simplest disguise attempts, and it is the reason renaming a shell to shell.jpg does not work against a stock WordPress install: the content check sees PHP code where an image should be, and the type is discarded.
The third check is the capability. If the type or extension is still empty after the first two checks, the upload is refused unless the account holds the unfiltered_upload capability. On a default install, nobody holds that capability, administrators included. The only way to grant it is to define the ALLOW_UNFILTERED_UPLOADS constant as true in wp-config.php, and on multisite only a super admin can hold it even then. That constant is the escape hatch, and the rule of this guide is simple: never define it. It lets administrators upload any file type at all, with no sanitizing, and it turns every admin account into a potential upload vector.
There are two gaps worth knowing about, because the rest of this guide exists to close them. The first gap is that these checks only run on uploads that go through WordPress. A plugin or theme that handles uploads with its own code can skip them entirely, and many do: they check the extension with a simple string comparison, or they check nothing at all, and they write the file straight into uploads. A vulnerability in any one of those handlers is a hole in the wall, and you cannot audit every plugin you run. The second gap is that the checks validate the file at the moment of upload, not at the moment it is served. A file that passed the checks can still be dangerous if its type is executable in the browser: an SVG with a script inside it passes no content check, because WordPress does not read SVG contents at all, and a .htaccess file, if one ever lands in uploads, is read by Apache on every request. The server-level execution block closes both gaps at once, because it does not care how the file got there or what WordPress thought of it. It only cares that no script runs from that directory.
One more fact shapes the hardening below. WordPress stores uploads under wp-content/uploads by default, organised into year and month subfolders, and the whole tree is served directly by the web server: any file in it can be requested by URL. Multisite keeps its uploads inside the same tree, under uploads/sites/, so one rule on the uploads directory covers every site on the network. Some sites move the uploads directory with the UPLOADS constant, and some plugins use their own subfolders, but the principle is the same everywhere: every directory that accepts user files needs the same execution block. When you apply the rules below, apply them to every writable directory your site has, not just the default one.
How an Upload Becomes a Shell: Five Attack Paths
Knowing the five ways an upload turns into code tells you which defences matter and which ones are theatre. Each path below has actually been used against WordPress sites, and each one is stopped by a different layer of the hardening in this guide.
1. The direct PHP upload
The simplest path is also the most common in vulnerability reports: a plugin or theme accepts a file upload and does not check the extension properly, or at all. The attacker uploads shell.php, and the server runs it when requested. The check that fails is usually in the plugin, not in WordPress core: a form plugin that validates the extension on the client side with JavaScript and never re-checks on the server, a theme options page that accepts a file upload for a logo and saves whatever arrives, an import feature that trusts the file name. Client-side validation is not validation. Anything the browser checks, the attacker can skip by sending the request directly. The server-side execution block is the defence here, because it does not depend on the plugin’s check. Even a perfectly uploaded shell.php becomes a text file nobody can run.
2. The double extension
Apache has a behaviour that surprises people who have never met it: when a file has more than one extension, Apache works through them from right to left, and if the last extension is one it does not recognise, it falls back to the earlier ones. A file named photo.php.123test or photo.php.xxx can be treated as a PHP file, because Apache does not know the final extension and moves on to .php. Some upload handlers make this worse by stripping only the last extension, or by blocking .php but forgetting .php5, .phtml, and .phar. The defence is to match the execution block against any script extension anywhere in the name, and to make the match case-insensitive, so .PHP, .Php, and .pHp are all caught. The FilesMatch pattern in the next section does exactly that, and the Nginx pattern matches the extension at the end of the request path regardless of what came before it.
3. The disguised image, or polyglot file
A polyglot is a file that is two things at once: a valid image and valid PHP. The classic example starts with the bytes GIF89a, the magic header of a GIF file, followed by PHP code. Image checks that only look at the first few bytes, or that call getimagesize(), see a GIF and approve it. If the file is saved with a .php extension, or if the server can be made to treat it as PHP, the code runs. WordPress core’s content check, which uses fileinfo on the whole file, is better than a magic-byte check, but a plugin that rolls its own validation with getimagesize() alone is fooled. The defence, again, is the execution block: a polyglot that cannot execute is just a broken image. The second defence is content validation that reads the whole file, which is what fileinfo does, and which your upload handlers should rely on instead of home-grown checks.
4. The uploaded .htaccess file
This is the path people forget, and it is the most dangerous one on Apache, because an uploaded .htaccess file can rewrite the server rules for the whole directory. If an attacker can upload a file named .htaccess containing a line that re-enables the PHP handler, or that maps a harmless extension to PHP, every defence in this guide can be undone from inside the uploads folder. WordPress core does not allow .htaccess uploads through the media library, but a vulnerable plugin might, and an attacker who already has a shell elsewhere can write one directly. The defence has three parts: the execution block should also deny access to .htaccess files themselves, so they can never be read back; uploads should never be world-writable, so a local process cannot drop files into the folder; and the server-level rule, placed in the virtual host configuration rather than in a .htaccess file, cannot be overridden by an uploaded .htaccess at all. Server-level rules beat per-directory rules for exactly this reason, and the guide shows both so you can use whichever your hosting allows.
5. The SVG with a script inside it
SVG is the file type that breaks the mental model, because it looks like an image and behaves like a document. An SVG file is an XML document, and the SVG specification allows it to carry <script> elements, event handler attributes such as onload, onclick, and onmouseover, <style> blocks, external references, and <foreignObject> elements that can host arbitrary XHTML. When a browser opens an SVG directly, it parses that document and runs the script, on your domain, with access to the session of whoever opened it. If a logged-in administrator opens a malicious SVG from your uploads folder, the script runs as that administrator. That is stored cross-site scripting, delivered as an image file, and it is the reason WordPress blocks SVG uploads by default. The file-types section below covers how to allow SVGs safely if you genuinely need them, and the short version is: sanitize every SVG on upload, restrict SVG uploads to trusted roles, and serve them with a Content Security Policy that neutralises anything the sanitizer misses. Never enable SVG uploads with a one-line upload_mimes snippet and nothing else.
Step 1: Block PHP Execution in wp-content/uploads
This is the single highest-impact step in the whole guide, and it takes five minutes. The rule belongs at the web-server layer, in the server configuration or in a .htaccess file inside the uploads directory, and it says one thing: no script file inside this directory may ever be executed or served as code. Apply it first, before the WordPress-level controls, because it protects you even when a plugin has a vulnerability you do not know about, and even when an attacker finds a way past every upload check. The exact rule depends on your server. Pick the one that matches your stack, and then run the test at the end of this section to prove it works.
Apache: the .htaccess rule
On Apache, create a file named .htaccess inside wp-content/uploads and put this in it:
# Block direct execution of scripts in uploads.
# This file lives in wp-content/uploads and applies to that
# directory and everything below it.
<FilesMatch "\.(?i:php|phtml|phps|phar|cgi|pl|py|asp|aspx|sh|shtml|shtm)$">
Require all denied
</FilesMatch>
# Belt and braces: on servers where PHP runs inside Apache
# (mod_php), switch the PHP engine off in this directory.
# This block does nothing on PHP-FPM servers, where PHP runs
# as a separate process, and that is fine: the FilesMatch
# block above is the rule that does the work there.
<IfModule mod_php.c>
php_admin_flag engine off
</IfModule>A request for shell.php in the uploads folder now returns 403 Forbidden instead of executing. The (?i:...) makes the match case-insensitive, so .PHP and .Php are caught too. The extension list is deliberately wide: php and its siblings phtml, phps, and phar, plus cgi, pl, py, asp, aspx, sh, shtml, and shtm, because any of those can be executable on some server configuration, and the cost of blocking a type nobody should ever upload is zero.
Three Apache facts trip people up, so check them before you assume the rule is live. First, .htaccess files only work if the virtual host allows them: the AllowOverride setting must permit the directives you are using, and its default value is None, which means distributed configuration is ignored entirely. If your host sets AllowOverride None, the .htaccess file is silently ignored, and you need the server-level <Directory> block shown below, or you need to ask your host to enable overrides for the uploads directory. Second, the Require directive needs AllowOverride AuthConfig on that folder. Third, php_admin_flag engine off only works when PHP runs inside Apache as mod_php. On the far more common modern setup, Apache with PHP-FPM, that line does nothing at all, because Apache hands .php requests to a separate PHP process and never runs the engine itself. That is expected. The FilesMatch block is the rule that protects a PHP-FPM server, and it works on both.
If you can edit the server or virtual host configuration, put the rule there instead of in a .htaccess file. A server-level rule cannot be overridden by an uploaded .htaccess file, which closes attack path 4 above, and it cannot be deleted by a compromised WordPress install. Adjust the path to your site:
<Directory "/var/www/allabouthtml.com/public_html/wp-content/uploads">
<FilesMatch "\.(?i:php|phtml|phps|phar|cgi|pl|py|asp|aspx|sh|shtml|shtm)$">
Require all denied
</FilesMatch>
</Directory>To create the .htaccess file, use cPanel File Manager, browse to wp-content/uploads, create a new file named .htaccess, and paste the block above. Enable Show Hidden Files first, or you will not see the file afterward, because files starting with a dot are hidden by default. Over SSH, run cd wp-content/uploads and edit .htaccess with your editor of choice. Either way, keep the rule out of the root .htaccess file that WordPress manages: WordPress rewrites its own section on permalink changes, and a rule buried inside that section is hard to audit later.
Nginx: the location block
Nginx ignores .htaccess files completely, so the rule lives in your server block, and it must sit before the PHP handler location so that it is evaluated first. Add this inside the relevant server { } block, then run nginx -t to test the configuration and nginx -s reload to apply it:
# Deny access to any files with a .php extension in the uploads directory.
# WordPress publishes this restriction in its Nginx handbook, written to
# cover requests for PHP files beneath uploads or files, including
# subdirectory installs and multisite.
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}For broader protection that also catches the sibling extensions, use the wider pattern:
location ~* /wp-content/uploads/.*\.(?:php|phtml|phps|phar|cgi|pl|py)$ {
deny all;
return 403;
}Order matters on Nginx. Regex locations are checked in the order they appear, and the search stops at the first match, so a broad PHP handler location placed above this block would catch the request first and execute the file. Put the uploads denial above the location ~ \.php$ block that hands requests to PHP-FPM. The Nginx approach is stronger than .htaccess in one important respect: the configuration is not in the web root, so a compromised WordPress installation cannot modify it, and no uploaded file can override it. The common surprise for people moving from Apache to Nginx is assuming their hardening came with them. It did not. An Nginx site needs its own rule, in the server configuration, and an .htaccess file in uploads does nothing at all there.
IIS: the web.config rule
On IIS, the equivalent lives in a web.config file inside wp-content/uploads:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="PHP" />
</handlers>
</system.webServer>
</configuration>Removing the PHP handler mapping for that directory means IIS has no way to execute a PHP file found there, and the request falls through to a static file download or a 404. IIS is rare for WordPress, so this is a short section, but the principle is identical: the rule lives in the server configuration for that directory, and it denies execution rather than trusting the upload check.
Also lock the other writable directories
wp-content/uploads is the main target, but it is not the only writable directory. Any folder a plugin or theme writes user files into deserves the same block: cache folders, form upload folders, import folders, and any custom directory a plugin creates under wp-content. The quick way to find them is to list directories under wp-content that are writable by the web server user and ask whether each one should ever contain executable code. The answer is almost always no. Apply the same .htaccess file or the same Nginx location pattern, adjusted for the path, to each one. A shell in a cache folder is just as dangerous as a shell in uploads, and attackers do look there once the obvious folder is locked.
Test the rule before you trust it
A rule you never tested is a rule you do not have. Create a harmless test file in the uploads folder and request it. Over SSH, run:
echo "<?php echo 'EXECUTED';" > wp-content/uploads/test-php-block.php
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/wp-content/uploads/test-php-block.phpYou want a 403. If you see 200 and the word EXECUTED, the rule is not live: check AllowOverride on Apache, check the location order on Nginx, and check that you edited the right server block. Then test the variants: rename the file to test-php-block.PHP in capitals, to test-php-block.phtml, and to test-php-block.php.123test, and confirm each one is denied too. When every variant returns 403, delete the test files and move on. Keep the test in your notes, because you should repeat it after every server migration, every hosting change, and every major WordPress update, all of which can silently drop a per-directory rule.
Step 2: Restrict File Types to a Strict Allow-List
Blocking execution is the safety net. Restricting file types is the front gate, and it is worth doing properly, because it stops most bad files from ever reaching the disk. The principle is simple and it comes straight from the OWASP file upload guidance: allow only the extensions you need, validate content rather than trusting the extension, and restrict who may upload. A deny-list, where you block the extensions you can think of, always loses, because there is always one more executable extension you forgot. An allow-list, where only named types pass, fails closed: anything you did not explicitly allow is refused.
WordPress gives you the allow-list already, and the job is to keep it small. The upload_mimes filter controls the list of permitted types, and you can use it to remove anything your site does not need. The code below belongs in a small must-use plugin, wp-content/mu-plugins/upload-hardening.php, rather than in your theme’s functions.php, so it survives theme changes and cannot be disabled from the dashboard by an attacker who gains admin access. Must-use plugins load automatically and do not appear in the normal plugin list.
<?php
/**
* Plugin Name: Upload Hardening
* Description: Restrict uploadable file types to a strict allow-list.
*/
// Remove file types this site never needs to accept.
add_filter( 'upload_mimes', 'aah_restrict_upload_mimes' );
function aah_restrict_upload_mimes( $mimes ) {
// Executable and scriptable types: never allow these.
unset( $mimes['swf'] );
unset( $mimes['exe'] );
unset( $mimes['htm'] );
unset( $mimes['html'] );
unset( $mimes['js'] );
unset( $mimes['svg'] );
// Office macros and archives your site never accepts:
// remove the lines below only if you genuinely need them.
unset( $mimes['zip'] );
unset( $mimes['doc'] );
unset( $mimes['docx'] );
return $mimes;
}Two warnings about that snippet. First, do not remove a type your site actually uses: if your forms accept PDF applications, keep pdf in the list, and if your authors upload ZIP files of assets, keep zip. The allow-list should match the site, not a generic template. Second, never go the other direction and add dangerous types through this filter. The one-line snippet that adds svg to upload_mimes for every logged-in user, with no sanitizing at all, is the most copied bad security advice on the WordPress internet, and it is the exact hole that turns a subscriber account into a stored cross-site scripting vector on any site with open registration.
SVG uploads, done safely
If your site genuinely needs SVG logos or icons in the media library, there are three safe choices, and a bare upload_mimes snippet is not one of them. The first choice is a maintained plugin: Safe SVG on WordPress.org sanitizes uploaded SVGs with the svg-sanitize library and can restrict uploads by role. The second choice is to allow the SVG type for administrators only and sanitize every file on upload with the same library, in your own must-use plugin, using three hooks: upload_mimes to allow the type for trusted users, wp_handle_upload_prefilter to sanitize the temporary file before WordPress moves it into uploads, and wp_check_filetype_and_ext to handle servers whose MIME detection reports SVG as plain text. The third choice is to keep SVGs out of the media library entirely and ship them with your theme, where they are files you wrote, not files a user uploaded. All three are covered in depth by the SVG guides linked in the sources for this post. Whichever you choose, add a Content Security Policy that neutralises anything the sanitizer misses, and audit the SVGs already sitting in your uploads folder, because a sanitizer only cleans files uploaded after it was installed.
Restrict who can upload at all
File-type rules are only half the story. The other half is who gets to use the upload feature in the first place. On a default WordPress install, the upload_files capability belongs to roles from Contributor upward, and on sites with open registration, every new account arrives with the Subscriber role, which cannot upload. That is a sane default, and you should keep it that way. Audit your roles and ask, for each one, whether the people in it genuinely need to upload files. A membership plugin that hands the upload capability to every registered customer has just given every stranger on the internet an upload vector. A form plugin that lets anonymous visitors attach files has done the same. If a feature needs anonymous uploads, treat those files as hostile by default: store them outside the web root where possible, scan them, rename them, and never serve them from the uploads directory with their original name.
The same thinking applies to the unfiltered_html capability, which controls who may post raw HTML, and to the unfiltered_upload capability, which controls who may upload any file type. Both are powerful, both are granted sparingly by core, and both are worth checking after installing any plugin that manages roles, because role-management plugins are the usual way these capabilities leak to roles that should never hold them. If you find ALLOW_UNFILTERED_UPLOADS defined as true in wp-config.php, remove it. It was probably added years ago to solve a one-off upload problem, and it has been silently letting administrators upload any file type ever since.
Step 3: Make Uploaded Files Boring
Even with the execution block and the allow-list in place, there is a third layer that costs almost nothing: make the uploaded file itself uninteresting to an attacker. A shell is dangerous because it has a predictable name, a predictable location, and an executable extension. Take those away and the file becomes inert data even if every other defence fails.
The most effective version of this is to rename files on upload. WordPress already renames files that collide with an existing name, appending a number, but it keeps the original name otherwise, and the original name is attacker-controlled. A plugin or a small filter can replace the uploaded name with a random string and keep only the safe extension, so shell.php becomes something like a7f3c9e2b41d.jpg, and an attacker who somehow gets a file onto the disk cannot guess its URL. If you run a site where users upload files, randomised names are one of the highest-value changes you can make, and they also stop the information leak of original file names, which sometimes contain usernames, client names, or internal project titles.
If randomised names are too disruptive for your workflow, do the lighter version: strip executable extensions from the name at upload time, force the extension to lowercase, and reject any file whose name contains more than one dot before the final extension. That single check kills the double-extension path, because photo.php.jpg never reaches the disk under a name the server could mistake for code. Whichever version you choose, keep the original extension validation as well: renaming is a third layer, not a replacement for the allow-list or the execution block.
The strongest version of this idea is to store uploads outside the web root entirely, so no uploaded file can ever be requested by URL. WordPress does not support that out of the box, because the media library expects files under the document root, and moving them requires a plugin or custom code that serves files through a PHP script after checking permissions. For most sites that is more engineering than the risk justifies, and the execution block plus the allow-list is enough. But if your site accepts uploads from anonymous visitors, or from a large number of untrusted accounts, serving files through a script instead of directly from disk is worth considering, because it turns the uploads folder from a public directory into a private store with a gatekeeper.
Step 4: Harden the Rest of the Upload Path
Upload security is a chain, and the execution block is only one link. These are the remaining links, each small, each worth doing on the same afternoon.
File permissions. Directories 755, files 644, wp-config.php 600 to 640, and never 777 anywhere. A world-writable uploads folder lets any local process, including a compromised script on a neighbouring site on shared hosting, drop files into it. Check your permissions with your host’s file manager or over SSH, and fix anything that shows 777. The WordPress hardening guide covers permissions in full, and it is the same list: the uploads folder needs to be writable by the web server user, and by nobody else.
Turn off the file editor. Add define( 'DISALLOW_FILE_EDIT', true ); to wp-config.php. The dashboard file editor lets any administrator edit theme and plugin files from the browser, which turns a stolen admin session into code execution without any upload at all. The constant disables it. While you are in wp-config.php, check that DISALLOW_UNFILTERED_HTML is not needed for your setup, and confirm that no old debugging constant is still defined. The full wp-config.php hardening list, including moving the file one directory above the web root, is covered in the Day 29 post in this series, and it pairs directly with this one.
Keep PHP patched. The November 2026 PHP releases fixed critical flaws that could turn a file upload into remote code execution, including CVE-2026-115560, a session serializer flaw triggered through the upload progress feature. If your server still runs an unpatched PHP branch, an attacker does not need to upload a shell at all: the upload feature itself can be the exploit. Check your PHP version, update to a supported branch, and read the Day 23 post in this series for the safe way to check and update the version on a WordPress site. The CVE-2026-115560 post explains the vulnerability itself, and the PHP session hardening post from Day 26 covers the configuration that makes the next flaw harder to exploit.
Let a security plugin or firewall enforce the rule. If editing server files feels risky, a reputable hardening plugin or a web application firewall can apply the same no-PHP-in-uploads rule and keep it in place through updates. This is a reasonable choice on managed hosting where you cannot edit the server configuration. Pick the plugin the way the Day 22 checklist in this series describes: a maintained security plugin from a vendor you can name, configured deliberately, not three overlapping plugins fighting over the same rule. A plugin is a good enforcement mechanism for the execution block, but it is not a substitute for it: if the plugin is disabled, the rule goes with it, which is why the server-level rule remains the preferred option wherever you can apply it.
Keep plugins and themes updated, and audit them. The upload handler that gets compromised is almost always a plugin, not core. The Day 24 audit in this series shows how to check your plugins for known vulnerabilities, remove the ones you no longer use, and replace the ones that are abandoned. Every plugin you remove is an upload handler you no longer have to trust. Pay particular attention to form plugins, media plugins, import tools, and anything that advertises file uploads as a feature, and check their changelogs for security fixes before you update, so you know which of them has had upload problems before.
Step 5: Verify It Actually Works
Hardening you never tested is hardening you do not have. Run these five checks after applying the rules above, and repeat them after every server migration, hosting change, or major WordPress update, because all three can silently drop a per-directory rule.
1. The direct request test. Upload a harmless test file, then request it and confirm a 403:
echo "<?php echo 'EXECUTED';" > wp-content/uploads/test-php-block.php
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/wp-content/uploads/test-php-block.phpYou want 403. If you see 200, the rule is not live. On Apache, check AllowOverride in the virtual host and confirm the .htaccess file is in wp-content/uploads, not in the site root. On Nginx, check that the uploads denial sits above the PHP handler location in the server block, and run nginx -t to confirm the configuration is valid. Then delete the test file.
2. The variant test. Rename the test file to test-php-block.PHP in capitals, to test-php-block.phtml, and to test-php-block.php.123test, and confirm each one is denied too. This is the test that catches a case-sensitive pattern and a pattern that only matches .php at the very end of the name. If any variant returns 200, widen the pattern to the case-insensitive FilesMatch block or the wider Nginx location shown in Step 1.
3. The upload test. Log in as a low-privilege test user, or use a test form, and try to upload a file with a .php extension, a file with a double extension, and a text file renamed to .jpg. The first two should be refused by the file-type check, and the third should be refused by the content check. If any of them succeeds, find out which handler accepted it: it is either a plugin with its own upload code, or a role with the unfiltered_upload capability, and both need fixing.
4. The permissions audit. Over SSH, list the uploads tree and look for anything world-writable or executable:
find wp-content/uploads -type d -perm 777
find wp-content/uploads -type f -name "*.php"
find wp-content/uploads -type f -name "*.phtml"The first command should return nothing. The second and third should return nothing either, unless you have a legitimate reason for PHP files in uploads, which you almost certainly do not. If they return files, inspect each one before deleting it: a .php file in uploads is either a leftover from a plugin that should not be there, or a shell.
5. The headers check. Request a normal image from uploads and look at the response headers. The image should be served with its correct content type, and if you set a Content Security Policy for uploaded files, it should be present. This is the check that catches a rule that is too broad: a FilesMatch pattern that accidentally blocks images will break your media library, and you want to find that out in testing, not from a reader reporting broken images.
If a Shell Is Already There
Finding a shell is not the end of the incident. It is the beginning, because a shell is almost never the only thing the attacker left behind. Do not just delete the file and move on. Work through this list, in order.
Take the site offline, or at least stop the bleeding. Put the site in maintenance mode, block the attacker’s IP addresses if you know them, and change the database password and the WordPress salts in wp-config.php immediately. The salts change invalidates every logged-in session, including the attacker’s. Do this first, before cleanup, because every minute the shell is live is a minute the attacker can use it to dig deeper.
Find everything the attacker left. Search the whole install for recently modified files, for PHP files in directories that should never contain them, and for the classic shell markers: eval, base64_decode, shell_exec, system, passthru, and assert in files that should not contain them. The two find commands from the verification section are the start. Also check for new administrator users, new plugins you did not install, scheduled tasks, and modified theme files. A competent cleanup finds all of it. A quick cleanup finds the one file you noticed and misses the backdoor in the theme.
Restore from a clean backup. If you have a backup from before the compromise, restore it, and then re-apply the hardening in this guide before bringing the site back online. Restoring without hardening just recreates the hole the attacker walked through. If you do not have a clean backup, rebuild the install: fresh WordPress core, fresh themes and plugins from their official sources, and your content and uploads carried over only after inspection. The backup and file-integrity routine that makes this possible is the Day 28 post in this series.
Rotate every secret. Database password, WordPress salts, hosting panel password, FTP and SSH credentials, API keys stored in the site, and the passwords of every administrator account. Assume the attacker read wp-config.php, because a shell running as the web server user can, and act accordingly.
Find the entry point and close it. The shell tells you where it landed, not how it got in. Check the access logs for the upload request, find which plugin or form accepted it, and update, replace, or remove that code. Then apply the execution block from Step 1, because the next vulnerability in the same plugin will land in the same folder, and the block makes it harmless. Report the vulnerability to the plugin author if it is a genuine flaw, and check the plugin-vulnerability audit from Day 24 for the routine that keeps this from happening again.
Mistakes That Undo Upload Hardening
| Mistake | Why it breaks the hardening | What to do instead |
|---|---|---|
Blocking PHP with php_flag engine off only | That setting works on mod_php only. On Apache with PHP-FPM, which is the common modern setup, it does nothing, and the uploads folder stays executable. | Use the FilesMatch deny block, which works regardless of how PHP runs, and keep the php_flag line only as a second layer. |
Putting the rule in the root .htaccess | WordPress rewrites its own section of the root file on permalink changes, and a rule buried there is hard to audit. Worse, a rule in the root file does not clearly scope to uploads. | Put the .htaccess file inside wp-content/uploads itself, or use a server-level <Directory> block. |
| Using a deny-list of extensions | Attackers always find the extension you forgot: .phtml, .phar, .php7, a double extension, a case variation. A deny-list fails open. | Use an allow-list of the types your site needs, and block execution of everything else at the server layer. |
Defining ALLOW_UNFILTERED_UPLOADS | It lets administrators upload any file type at all, with no sanitizing, and it turns every admin account into an upload vector. | Never define it. If a specific type is needed, allow it narrowly through upload_mimes for the roles that need it, with sanitizing. |
| Enabling SVG with a one-line snippet | An SVG can carry scripts and event handlers. An unsanitized SVG in uploads is stored cross-site scripting waiting for an administrator to open it. | Use a maintained sanitizing plugin, restrict SVG uploads to trusted roles, and serve them with a Content Security Policy. |
| Trusting client-side validation | JavaScript checks in the browser can be skipped entirely by sending the request directly. A form that validates only in the browser validates nothing. | Validate on the server, every time, and keep the execution block as the layer that does not depend on validation at all. |
| Forgetting the other writable folders | Cache folders, form upload folders, and plugin-specific folders accept files too. A shell in a cache folder is just as dangerous as one in uploads. | Apply the same execution block to every writable directory under wp-content. |
| Never testing the rule | AllowOverride None silently ignores an Apache .htaccess file, and a misplaced Nginx location never matches. An untested rule is a guess. | Upload a test file, request it, confirm 403, test the case and double-extension variants, then delete the test files. |
| Deleting a shell and stopping there | A shell is almost never the only thing left behind. There is usually a second backdoor, a new admin user, or a modified theme file. | Take the site offline, restore from a clean backup, rotate every secret, and find the entry point. |
Frequently Asked Questions
1. Does blocking PHP in uploads break my media library?
No. The media library serves images, PDFs, video, and audio, none of which are PHP files. The execution block only denies requests for script extensions inside the uploads directory. Images, documents, and media keep working exactly as before. If an image stops loading after you apply the rule, the pattern is too broad: check that it only matches script extensions, and re-run the headers check in the verification section.
2. My host uses Nginx. Do I still need the WordPress-level controls?
Yes. The Nginx location block is the safety net, and the WordPress controls are the front gate. The block stops a shell from executing, but it does not stop a bad file from being stored, and a stored SVG or a stored .htaccess can still cause damage without ever executing as PHP. Keep the allow-list, keep the role limits, and keep the execution block. They cover different failures.
3. What if my host will not let me edit server configuration or .htaccess?
Use a reputable hardening plugin or a web application firewall to enforce the same rule. Many managed hosts also apply the no-PHP-in-uploads rule themselves: ask your host whether it is already in place, and ask them to confirm it with the same test from the verification section. If your host cannot confirm it and will not let you apply it, that is a reason to consider a different host, because this is a basic WordPress hardening control.
4. Can I just use a security plugin instead of editing server files?
A security plugin can enforce the rule, and for many site owners it is the practical choice. The trade-off is that the rule lives inside WordPress, so it goes away if the plugin is disabled, and it does not protect you during the window between a compromise and the plugin noticing. The server-level rule has neither weakness. Use the plugin if you must, but prefer the server rule wherever your hosting allows it.
5. Are file uploads from contact forms safe?
Only if the form plugin validates on the server, restricts file types, and stores the files safely. A form that lets anonymous visitors attach files is an upload vector, and it deserves the same treatment as the media library: a strict allow-list, randomised file names where possible, and the execution block on the folder the files land in. Check where your form plugin stores attachments, and apply the block there too.
6. Why does WordPress block SVG uploads by default?
Because an SVG is an XML document that can carry scripts, event handler attributes, style blocks, and embedded markup. When a browser opens an SVG directly, it runs that code on your domain, with the session of whoever opened it. If an administrator opens a malicious SVG from your uploads folder, the script runs as that administrator. WordPress blocks SVG uploads to prevent exactly that, and the safe ways to enable them are sanitizing on upload, restricting them to trusted roles, and serving them with a Content Security Policy.
7. I found a .php file in my uploads folder. Is my site hacked?
Not necessarily, but treat it as a compromise until you prove otherwise. Some plugins legitimately place PHP files in uploads, though very few do, and most have no reason to. Inspect the file, check when it was created and what it contains, and compare it against the plugin that put it there. If you cannot explain it, assume it is a shell and follow the incident response section: take the site offline, restore from a clean backup, rotate every secret, and find the entry point.
Conclusion
WordPress file upload security comes down to one idea, applied at three layers: an uploaded file is untrusted input, and it must never be treated as code. The server layer says it most bluntly, by refusing to execute any script inside wp-content/uploads, on Apache with a FilesMatch block, on Nginx with a location block, and on IIS by removing the PHP handler. The WordPress layer says it by restricting file types to a strict allow-list, by refusing uploads from roles that should never upload, and by never defining ALLOW_UNFILTERED_UPLOADS. The file layer says it by renaming uploads, stripping executable extensions, and keeping permissions tight. Any one of those layers can fail, and a site with all three still stands.
Do the server rule today. It takes five minutes, it needs no plugin, and it closes the attack path that turns a plugin vulnerability into a full site takeover. Then run the five verification tests, so you know the rule is live and not just written down. Then work through the rest of the checklist at your own pace: the allow-list, the role audit, the permissions, and the plugin audit from the earlier posts in this series. The uploads folder will keep accepting files from the outside world, because that is its job. After this guide, what it accepts can no longer run.
Related Articles You’ll Love
- WordPress Security Plugin Checklist After PHP2Shell — the maintained-plugin routine that enforces upload rules like this one and keeps them in place through updates.
- WordPress Plugin Vulnerabilities: Audit, Remove, Replace — how to find the vulnerable upload handler in your own install, and the audit routine that keeps it from happening again.
- XSS Prevention for WordPress: A Practical Checklist — the stored cross-site scripting angle, including malicious SVGs and the Content Security Policy that neutralises them.
- PHP Session Security Hardening After CVE-2026-115560 — the follow-up to the November PHP security release, and the session configuration that makes the next upload-triggered flaw harder to exploit.
- CVE-2026-115560: PHP RCE Vulnerability and Fix — the critical flaw that could turn a file upload into remote code execution, and the patch that fixed it.


No comment