File Uploads
Forms with enctype multipart/form-data fill $_FILES. Check the error code, size and real MIME type, generate a safe name and move with move_uploaded_file. Shown as code because it needs a web request.
Example
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["photo"])) {
$f = $_FILES["photo"];
if ($f["error"] === UPLOAD_ERR_OK && $f["size"] < 2_000_000) {
$ext = pathinfo($f["name"], PATHINFO_EXTENSION);
$safe = bin2hex(random_bytes(8)) . "." . $ext;
move_uploaded_file($f["tmp_name"], __DIR__ . "/uploads/" . $safe);
}
}Exercise
Move an upload.
($tmp, $dest)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.