Stop PHP from running in your website's uploads folder
Short answer: Your uploads folder should hold images, PDFs and other files, never run code. If an attacker manages to upload a PHP file (a "web shell") through a vulnerable form or plugin and the server executes it when requested, they control your site. Block script execution in upload directories at the web server, stop per-folder configuration overrides there, keep uploads outside the web root or behind a handler where you can, and restrict what can be uploaded in the first place. If you find PHP files in uploads today, treat it as a compromise, not just a cleanup.
Why this matters
On a typical PHP site, any file ending in .php inside the public web folder is executed by the server when someone requests its URL. Upload folders are writable by the web server so that customers or editors can add files. Put those two facts together and a single weak upload form, or a plugin flaw that lets someone write a file, becomes full remote code execution.
OWASP's File Upload Cheat Sheet recommends storing uploaded files on a different server, or failing that outside the web root, and serving public files through a handler that maps an ID to a stored file rather than exposing the real path. It also warns that attackers try to upload configuration files that change how a directory handles file types, for example mapping a harmless-looking extension to be executed, and recommends disabling per-directory configuration overrides on upload directories, such as Apache's AllowOverride None.
Step 1: Look for code where it shouldn't be
On your server or through your host's file manager, search upload directories for:
- files ending in
.php,.phtml,.php5,.pharor similar .htaccessor.user.inifiles you didn't put there- double extensions like
invoice.pdf.phporphoto.jpg.php - recently modified files with odd names
On WordPress, that means wp-content/uploads and any folders plugins created for uploads. If you find unexpected code, skip to Step 4 before tidying up.
Step 2: Stop execution at the web server
nginx. Make sure your PHP handling block only passes files to PHP-FPM from directories where application code lives, and add a location for the uploads path that returns 403 or 404 for script extensions. The safest pattern is to never hand any file under the uploads path to PHP at all.
Apache. In the server configuration (not only in an .htaccess file inside the uploads folder, which an attacker could overwrite), disable PHP handling for the uploads directory and set AllowOverride None there, as OWASP suggests, so a planted .htaccess can't re-enable it.
Managed hosting. Many hosts offer a setting or support request to disable PHP in specific folders. Ask for it explicitly.
Test afterwards: upload a harmless test file named test.php containing a line of plain text to the uploads folder through the server (not the site), request its URL, and confirm you get an error or a download, never executed output. Then delete it.
Step 3: Tighten the uploads themselves
- Allow only the file types you need, checked on the server by content as well as extension.
- Rename files on upload to random names with a safe extension you choose.
- Store private uploads outside the web root, such as customer documents, and serve them through your app with access checks.
- Set permissions sensibly. WordPress's hardening guide describes
/wp-content/as user-supplied content writable by your account and the web server, while application code such as plugins should be writable only by your user account. The web server shouldn't be able to modify your code. - Disable the dashboard file editor on WordPress with
DISALLOW_FILE_EDIT, which the hardening guide explains removes the ability to edit theme and plugin files from the dashboard.
Step 4: If you found a web shell
A PHP file in uploads that you didn't put there means someone had write access. Deleting it isn't enough:
- Take a backup of the current state for investigation, then put the site in maintenance mode if it handles payments or personal data.
- Find the entry point: check access logs for requests to the shell and the request that uploaded it, and update or remove the vulnerable plugin, theme or form.
- Look for other changes: new admin users, modified core or plugin files, scheduled tasks, injected scripts in the database.
- Restore code from clean sources, and keep only your uploads after scanning them.
- Change all credentials: admin accounts, database, hosting, FTP or SSH, and API keys stored in configuration files.
- Watch for re-infection for several weeks.
If you aren't confident doing this, bring in someone who is.
Monitor for new code files
Prevention fails occasionally, so add detection. A simple scheduled job can list any file with a script extension under upload directories and alert you if one appears. File-integrity monitoring tools, security plugins and some hosts can also alert on changed core or plugin files. Whatever you use, make sure alerts go to a person who will act on them the same day, not to an unread mailbox.
An illustrative example
A hypothetical Phoenix landscaping company's site had a quote form plugin that accepted file attachments. During a routine check, its developer found img_2391.php in the uploads folder. Logs showed it had been uploaded through an outdated version of the plugin and used to add an admin account. The developer restored code from clean sources, removed the account, updated the plugin, blocked PHP execution under uploads in the nginx configuration, moved quote attachments outside the web root and rotated every password and key.
FAQ
Will blocking PHP in uploads break my site?
Normally no. Legitimate sites don't run code from upload folders. Test after the change.
Is an .htaccess file in uploads enough?
It helps, but attackers who can write files can overwrite it. Prefer server-level configuration.
Do static site hosts have this problem?
Not in the same way, since they don't execute uploaded PHP. Upload handling in any app still needs care.
How often should I check?
After plugin updates and monthly, or automatically with file-change monitoring.
If you'd like your upload folders locked down or need help after finding unexpected files, tell us how your site is hosted.