1, 'error' => 'Method not allowed']); exit; } // 验证令牌 $token = $_POST['token'] ?? $_SERVER['HTTP_X_UPLOAD_TOKEN'] ?? ''; if ($token !== $SECRET_TOKEN) { http_response_code(403); echo json_encode(['code' => 1, 'error' => 'Invalid token']); exit; } // 检查文件是否上传 if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) { $error_msg = isset($_FILES['file']) ? 'Upload error: ' . $_FILES['file']['error'] : 'No file uploaded'; echo json_encode(['code' => 1, 'error' => $error_msg]); exit; } $file = $_FILES['file']; // 检查文件大小 if ($file['size'] > $MAX_SIZE) { echo json_encode(['code' => 1, 'error' => 'File too large (max 20MB)']); exit; } // 检查文件类型(通过扩展名) $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if ($ext !== 'pdf') { echo json_encode(['code' => 1, 'error' => 'Invalid file type. Only PDF allowed.']); exit; } // 生成文件名 $file_id = 'resume_' . bin2hex(random_bytes(6)); $filename = $file_id . '.pdf'; $filepath = $UPLOAD_DIR . $filename; // 确保目录存在 if (!is_dir($UPLOAD_DIR)) { mkdir($UPLOAD_DIR, 0755, true); } // 移动文件 if (move_uploaded_file($file['tmp_name'], $filepath)) { $url = $BASE_URL . $filename; echo json_encode([ 'code' => 0, 'url' => $url, 'file_id' => $file_id, 'filename' => $filename ]); } else { echo json_encode(['code' => 1, 'error' => 'Failed to save file']); }