<?php declare(strict_types=1); $message = ''; $messageType = ''; // 'success' or 'error' // Allowed MIME types mapped to safe extensions const ALLOWED_MIME_TYPES = [ 'image/jpeg' => 'jpg', 'image/png' => 'png', ]; const MAX_FILE_SIZE = 2 * 1024 * 1024; // 2 MB in bytes const UPLOAD_DIR = __DIR__ . '/uploads';
Write an image upload script in PHP 8: accept only JPG and PNG, reject files over 2 MB and check the type by reading the file's real MIME type. Save the file to an uploads folder under a random name and show the user a clear message when something goes wrong.
