false]; try { // Step 1: Check form fields $response['step'] = 'checking form fields'; $response['formFields'] = array_keys($_FILES); // Step 2: Find file fields $response['step'] = 'finding file fields'; $uploadedFiles = []; foreach ($_FILES as $fieldName => $fileInfo) { if (preg_match('/^file[0-9]+$/', $fieldName)) { $uploadedFiles[] = $fieldName; } } $response['uploadedFiles'] = $uploadedFiles; if (empty($uploadedFiles)) { throw new Exception('No files found'); } // Step 3: Create temp directory $response['step'] = 'creating temp dir'; $uploadDir = sys_get_temp_dir() . '/test_uploads'; if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true); $response['uploadDir'] = $uploadDir; // Step 4: Upload first file $response['step'] = 'uploading file'; $fieldName = $uploadedFiles[0]; $file = $_FILES[$fieldName]; $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'application/pdf']; if (!in_array($file['type'], $allowedTypes)) { throw new Exception('File type not accepted: ' . $file['type']); } $destPath = $uploadDir . '/' . basename($file['name']); move_uploaded_file($file['tmp_name'], $destPath); $response['uploadResult'] = true; $response['serverFile'] = basename($file['name']); $response['serverFileExt'] = pathinfo($file['name'], PATHINFO_EXTENSION); // Step 5: Read the file $response['step'] = 'reading file'; $response['filePath'] = $destPath; $response['fileExists'] = file_exists($destPath); // Step 6: Try image operations $fileExt = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if (in_array($fileExt, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) { $response['step'] = 'reading image'; $imageInfo = getimagesize($destPath); if ($imageInfo) { $response['imageWidth'] = $imageInfo[0]; $response['imageHeight'] = $imageInfo[1]; } $response['step'] = 'converting to base64'; $fileContent = file_get_contents($destPath); $base64Content = base64_encode($fileContent); $response['base64Length'] = strlen($base64Content); } // Step 7: Cleanup $response['step'] = 'cleanup'; unlink($destPath); $response['OK'] = true; $response['step'] = 'complete'; } catch (Exception $e) { $response['MESSAGE'] = $e->getMessage(); } jsonResponse($response);