Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
PHP Development

Uploads, Storage and Serving Files Safely

Last updated:

Uploads are a common attack route

A file uploaded into a directory the web server will execute is a straightforward route to running arbitrary code. It is one of the two most common ways PHP applications are compromised.

Storing uploads inside the web root is the mistake. Everything else about upload security is secondary to getting that one thing right.

Upload handling

  1. Store outside the web root, or in object storage
  2. Validate the content, not the filename extension
  3. Generate your own filename; never use the uploaded one
  4. Limit size and type, enforced server-side
  5. Scan where the files will be shared with others

Serving files

Files that should be private must be served through code that checks permissions, not by a direct URL. A guessable URL is not access control.

  • A controller that authenticates, authorises, then streams the file
  • No user input used to build the file path
  • Correct content type and disposition headers
  • Access logged where the documents are sensitive

Where to store them

OptionSuits
Local disk outside web rootSingle server, modest volume
Object storageAnything that may scale or needs redundancy
DatabaseRarely — small files only, and it complicates backups

Object storage is usually the right answer once you have more than one server or more than a few gigabytes.

Include them in backups

Uploaded files are frequently outside the database backup and therefore outside the backup entirely. A restore that recovers the records and not the documents is a partial restore.

Check specifically that your backup includes the file storage, and test a restore of both together.

Frequently asked questions

How do we validate file types?

Check the actual content, not the extension or the reported type. Both of those are supplied by the client and can be anything.

Should uploads be scanned?

Where files will be shared with other users, yes. For files only the uploader retrieves, the risk is lower.

What about very large files?

Upload directly to object storage with signed URLs, bypassing your application entirely. That avoids memory and timeout problems.

Are uploaded files in our backups?

Check specifically. They are commonly missed, and it is discovered during a restore.

Keep reading

Uploads stored in a public directory?

That is the single most exploitable configuration in a PHP application. Worth checking today.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Web DevelopmentCustom Software Development