Back to Coding

How Do You Handle File Uploads?

How Do You Handle File Uploads?
AI Tutorials Team 10 April 2026

An upload form is a few lines and it opens your application to arbitrary content from strangers. The gap between something that works and something safe is wider here than almost anywhere else, and generated implementations sit firmly at the working end.

What has to be checked?

  • Size. Enforced by the server, not only by the browser.
  • Type. Determined from the content rather than the file name.
  • Name. Never used directly as a path.
  • Content. A file claiming to be an image should actually parse as one.
  • Rate. How many, how often, per user.

Why is the file name dangerous?

Because it comes from the user and can contain path components, which is the classic route to writing a file somewhere you did not intend. Generating your own name and storing the original separately for display removes the problem entirely.

Why is the declared type unreliable?

Because the browser reports what the file claims to be, which anybody can change. Checking the actual content is the only meaningful validation, and it is one line with a library that inspects the file.

Where should files go?

Not in the application directory. Object storage from a hosting provider is the standard answer: it survives deployments, does not fill your server, and can serve files directly.

Storing uploads alongside code is the arrangement that quietly breaks when the application is redeployed, which is a common and surprising loss.

What is the serving problem?

That a file uploaded by one user may be readable by anybody who guesses the address. If uploads are private, they need an authorisation check before being served, and static hosting does not provide one.

Signed links with a short expiry are the usual arrangement and are supported by every major storage provider.

What does generated code typically produce?

An implementation that accepts a file, trusts its declared type, uses its name, saves it locally and serves it publicly. Every one of those is the convenient choice rather than the safe one.

Review this upload handler for: whether the size limit is enforced server side, whether the type is verified from the content, whether the user-supplied name can influence the storage path, and whether an uploaded file is readable by anybody who knows the address.

Serving uploads from a different domain than your application is worth doing where possible, since it limits what an uploaded file could do if it were served as something other than expected.

What about images specifically?

Worth processing rather than storing as received. Resizing to a maximum dimension, re-encoding, and stripping metadata all reduce risk and storage at once, and metadata frequently includes location information the person did not intend to share.

What about scanning for malware?

Worth considering if files are shared between users, since your application then becomes a distribution route. For files only the uploader retrieves, the risk profile is different and the case is weaker.

What about the cost of storage?

Small at first and permanent, which is a combination worth planning for. Uploads accumulate, nothing removes them when a record is deleted unless something is written to do that, and orphaned files outlive whatever they belonged to.

Deleting the stored file when the record referring to it goes is a few lines that nobody writes. Adding a size limit per user is the other measure worth having, since an application with unbounded uploads has an unbounded bill, and the person who discovers that is usually you at the end of a month.

What is the minimum worth doing?

Server-side size limit, content-based type check, generated file names, storage outside the application, and an authorisation check on retrieval. Five things, none difficult, and they cover almost everything that goes wrong with uploads.