Secure file upload handler with type checks and size limits
Writes or reviews a file upload endpoint so it accepts only the files it should: real type checks from file content, size and count limits, safe random file names, storage outside the web root and no way to run an uploaded file. Use it for profile pictures, document uploads, invoices and any form that takes attachments.
The output this prompt produced, unedited.
The prompt
What to give
Language and framework: [e.g. Python with FastAPI, or PHP 8.3 without a framework]
What users upload: [e.g. profile photos as JPEG, PNG or WebP up to 5 MB; invoices as PDF up to 10 MB]
Where files go: [e.g. a local folder, an S3-compatible bucket, a separate media domain]
Current code, if any: [paste, or new]
Who can download a file afterwards: [e.g. anyone with the link, or only the owner when signed in]
Check and build each point
Size limit enforced by the server and the web server, before the whole file is read into memory.
Type detected from the file's first bytes, not from the extension or the browser's content type, against an allow-list.
Images decoded and re-encoded, which strips metadata such as GPS location and drops any hidden payload.
The stored name is random; the original name is saved only as data and cleaned before display.
Files stored outside the web root, or on a domain or bucket that cannot execute scripts and serves files with a fixed content type and nosniff.
Downloads of private files go through an access check or a short-lived signed URL.
A limit on uploads per user per hour.
PDFs and office files are flagged for a virus scan step, with the hook shown even if no scanner is set up yet.
Errors tell the user what was wrong (too large, wrong type) without exposing paths.
Output format
Checklist: | Point | Pass, fail or not applicable | Note |, if existing code was given.
The complete handler code.
Web server or bucket settings needed, such as the body size limit and headers.
Tests: a valid file, an oversized file, a renamed executable with an image extension, an image with GPS metadata, a double extension such as photo.jpg.php.
Guardrails
Never trust the extension or the client's content type alone.
Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.
The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.