Lesson content
Read, practise, then check your understanding
Uploaded metadata and filenames are untrusted. Check transport error and size, then inspect content through finfo or actual decoding rather than extension or client MIME.
Practical example
<?php
$file = $_FILES['image'] ?? null;
if (!$file || $file['error'] !== UPLOAD_ERR_OK || $file['size'] > Limits::MAX_UPLOAD_BYTES) throw new RuntimeException();
$mime = (new finfo(FILEINFO_MIME_TYPE))->file($file['tmp_name']);
if (!in_array($mime, ['image/jpeg', 'image/png'], true)) throw new RuntimeException();
Generate server-side names, store outside executable/public paths, and use move_uploaded_file. Decode/re-encode images with resource limits, remove metadata where policy requires, scan risky formats, and serve with fixed content type plus safe download headers.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.