$status, 'message' => $message, 'data' => $data ]); exit; } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { json_response('error', 'Invalid request method.'); } if (!isset($_FILES['document']) || $_FILES['document']['error'] == UPLOAD_ERR_NO_FILE) { json_response('error', 'No file was uploaded.'); } $file = $_FILES['document']; // Check for upload errors if ($file['error'] !== UPLOAD_ERR_OK) { $upload_errors = [ UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', UPLOAD_ERR_NO_FILE => 'No file was uploaded.', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.', ]; $error_message = $upload_errors[$file['error']] ?? 'Unknown upload error.'; json_response('error', $error_message); } $upload_dir = __DIR__ . '/uploads/'; if (!is_dir($upload_dir)) { if (!mkdir($upload_dir, 0775, true)) { json_response('error', 'Failed to create upload directory.'); } } $file_name = basename($file['name']); $target_path = $upload_dir . $file_name; // Move the file to the uploads directory if (move_uploaded_file($file['tmp_name'], $target_path)) { json_response('success', 'File uploaded successfully.', ['filePath' => 'uploads/' . $file_name]); } else { json_response('error', 'Failed to move uploaded file.'); } ?>