Orofold Labs

Launching soon

Notes Get in touch

Engineering 4 min read Updated

Accepting file uploads safely in PHP

In short

Check every upload on the server, accept only the file types the application needs, and store the file where no visitor can run it or fetch it directly.

A file upload is input from a stranger, and it can carry more than data: a script, an archive that expands into something large, or a file name that points outside the folder you meant. Three decisions go wrong most often: trusting the browser, deciding what is allowed from the name alone, and storing uploads where anyone can run or download them. Use the checklist at the end before every release.

Check on the server, not in the browser

PHP makes the uploaded file available in the $_FILES array with the keys name, type, size, tmp_name, error and full_path. The manual describes full_path as available since PHP 8.1.0. The name, type and size keys come from the browser, so none of them is proof of anything; tmp_name and error are set by PHP:

  • The manual notes that the MAX_FILE_SIZE field is easy to fool on the browser side. Set limits on the server, where the PHP settings cannot be fooled.
  • The type value comes from the client and PHP does not check it. The manual warns not to take it for granted and suggests using it only as the first of a series of checks.
  • The name also comes from the client. The manual notes that basename() may prevent filesystem traversal attacks, but further validation of the file name may still be appropriate.
  • The temporary file is deleted at the end of the request unless you move or rename it. Check the error value before you use tmp_name at all.

Allow only what the application needs

The OWASP File Upload Cheat Sheet recommends an allowlist of extensions, limited to those the business actually needs. A blocklist of dangerous extensions is weak, because it is easy to miss one. Validate the extension after the file name has been decoded, so that encoded tricks cannot slip through.

An extension is only a label. The cheat sheet advises checking the real file content rather than the Content-Type header sent by the client, because that header can be spoofed. Signature checks help, but they should not stand alone. For images, the cheat sheet recommends decoding and re-encoding the file, which also strips metadata. It does not recommend accepting ZIP files. Set an upload size limit, and for archives also check the size after decompression.

Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea

Store files where they cannot be run

The cheat sheet prefers storing uploads on a different host. If that is not possible, keep them outside the web root. If files must sit inside the web root, make the directory write-only for the upload process and control read access through the application. In upload directories, disable per-directory configuration overrides such as .htaccess or web.config, so that a planted file cannot change how the server treats the folder.

Replace user-supplied file names with names generated by the application, such as a UUID or GUID. When a file must be public, map the identifier to the stored file inside the application. A file name shown back to visitors also needs escaping, as explained in the article on escaping output and stopping XSS.

Scan, limit and serve with care

Where it is available, run uploads through an antivirus engine or a sandbox. For formats such as PDF and DOCX, the cheat sheet suggests Content Disarm and Reconstruction (CDR). Public scanning services can leak the data you send them, so check that before you use one. Manual review in a sandbox is an option if resources allow.

Set limits on downloads too. Large or repeated requests can use up server resources, and the same concern applies to upload endpoints, which are covered in the article on rate limiting and abuse protection.

The cheat sheet gives no numeric thresholds for file sizes or name lengths. Choose the limits yourself, based on what the application needs, and write them down. This article summarises the OWASP cheat sheet and the PHP manual for orientation. It is not a security review of your code.

Sources and limits

Checklist before you ship

Use this list for every form that accepts a file:

  1. Check the error value, and confirm that the file size is within a limit set on the server.
  2. Check the extension against an allowlist, then check the real file content.
  3. Generate a new file name, and keep the original name only as data if you need it.
  4. Store the file outside the web root or on a different host, and disable configuration overrides in any upload folder that remains public.
  5. Move the file from tmp_name to its final location with move_uploaded_file() before the request ends.
  6. Scan the file where a scanner is available, and set limits on downloads. Test the form with files that should be rejected.

Common questions

Is it enough to check the file extension?
No. An extension is only a label, and the OWASP File Upload Cheat Sheet advises checking the real file content as well. The Content-Type value sent by the browser can be spoofed and should not be trusted.
Should uploads be stored inside the web root?
Preferably not. The cheat sheet prefers a different host, and if that is impossible, storage outside the web root. Files inside it should be write-only for the upload process.
Does OWASP give a maximum file size?
No. The cheat sheet asks for a file size limit but gives no numeric threshold, so choose a limit based on what the application needs.

Sources

  1. File Upload Cheat Sheet, OWASP Cheat Sheet Series. Accessed 24 November 2026
  2. Handling file uploads, PHP Documentation Group (php.net). Accessed 24 November 2026

Further reading

Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea

All notes