Accepting selected projects for Q2 2024 Check availability →
SERVER 02SYSTEM ONLINELAST SYNC: 03:17:44RSS 2.0 — ONLINE
HALF ASSEDTECHNICAL NOTES_
EST. 1999ISSUE 04.2BEST VIEWED AT 1024 × 768
TECHNICAL NOTES / PRACTICAL GUIDES / PHP & SECURITY / RECORD 0689fc
[PHP & SECURITY]GUIDE

Handling image uploads in PHP 4.3 and PHP 5

POSTED: 17.03.2005AUTHOR: ADMIN3 MIN READCOMMENTS: 0

Treat an uploaded photograph as untrusted input until the application has checked it. A file input can help visitors choose a picture, but neither the browser’s filename nor its reported content type establishes what arrived at the server.

Set limits before processing

Use a POST form with multipart/form-data and a named file control. Set upload_max_filesize and post_max_size deliberately, allowing room for the other form fields. Apply an application size limit too. A hidden MAX_FILE_SIZE field is not an enforceable security boundary. If the request exceeds post_max_size, PHP can leave both $_POST and $_FILES empty; report that case clearly.

Check the upload result

Inspect the error member of the expected $_FILES entry before using its temporary path. Distinguish no file selected from a partial or failed upload. Use is_uploaded_file when checking the temporary file and move_uploaded_file when moving it. Do not accept a client-supplied path as the name of a temporary upload. Reject unexpected arrays where a single file was requested.

Restrict the accepted image formats

For a photo feature, accepting only JPEG and PNG is easier to review than accepting every file a graphics package can export. Do not trust the supplied MIME type or extension. Check dimensions, enforce pixel and memory limits, then decode with an installed image library and reject failures. Keep that library patched. Re-encoding an accepted image provides a controlled output format but is not a substitute for limits.

Choose storage names on the server

Store originals outside the document root and use a server-assigned identifier with a fixed extension for derived images. Keep the original filename only as escaped descriptive metadata. Check for name collisions rather than overwriting an existing file. Where a public image directory is needed, ensure scripts cannot execute there and grant the application no more write access than it requires.

Test interrupted and concurrent submissions

Try a truncated image, a non-image with a picture extension, an oversized request and two uploads with the same original name. Test temporary-disk exhaustion and a failed final move. Publish the database record only after its file is in place, and remove abandoned temporary files without touching files still being processed. Tell the visitor when no image was saved.

DEPLOYMENT CHECK

Confirm the production PHP build has the required image-library support. A successful test on a developer’s workstation does not establish the formats or memory limits available on the hosting account.

References

PHP manual: POST method uploads

PHP manual: move_uploaded_file

PHP manual: getimagesize