..., 'filename' => ...] on success, or * ['error' => ..., 'status' => ...] on failure. */ function handleImageUpload($file) { if (!$file || $file['error'] !== UPLOAD_ERR_OK) { return ['error' => 'File upload failed', 'status' => 400]; } if ($file['size'] > MAX_UPLOAD_SIZE) { return ['error' => 'File too large. Maximum size is 5MB', 'status' => 400]; } $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp']; $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); if (!in_array($mimeType, $allowedTypes)) { return ['error' => 'Invalid file type. Only JPG, PNG, and WebP allowed', 'status' => 400]; } $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); $filename = generateUuid() . '.' . $extension; $filepath = UPLOAD_DIR . $filename; if (!move_uploaded_file($file['tmp_name'], $filepath)) { return ['error' => 'Failed to save file', 'status' => 500]; } return ['url' => '/api/uploads/' . $filename, 'filename' => $filename]; }