0, 'error' => ['message' => $msg]]); TraceProgram(__FILE__, __FUNCTION__, __LINE__, "fail: $msg", TR_EXIT); exit; } // Check if file is uploaded if (!isset($_FILES['upload']) || $_FILES['upload']['error'] !== UPLOAD_ERR_OK) { UploadFail('No file uploaded (error code ' . ($_FILES['upload']['error'] ?? 'none') . ')'); } $file = $_FILES['upload']; $allowedTypes = ['image/jpeg', 'image/jfif', 'image/png', 'image/gif']; // Validate file type if (!in_array($file['type'], $allowedTypes)) { UploadFail('Invalid file type'); } $filename = basename($file['name']); $targetPath = __DIR__ . '/inc/Uploads/' . $filename; TraceProgram(__FILE__, __FUNCTION__, __LINE__, "target: $targetPath"); if (!is_uploaded_file($file['tmp_name']) || !@copy($file['tmp_name'], $targetPath)) { $err = error_get_last(); UploadFail('Failed to save uploaded file: ' . ($err['message'] ?? 'unknown error')); } @unlink($file['tmp_name']); TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$filename"); echo json_encode([ 'uploaded' => 1, 'fileName' => $filename, 'url' => 'inc/Uploads/' . rawurlencode($filename) ]); TraceProgram(__FILE__, __FUNCTION__, __LINE__, "end", TR_EXIT); ?>