// 在 upload.php 中的文件上传处理部分 if ($action === 'upload_csv') { $db_name = $_POST['db_name'] ?? ''; $table_name = $_POST['table_name'] ?? ''; $user_id = $_POST['user_id'] ?? 0; $username = $_POST['username'] ?? ''; $overwrite_existing = $_POST['overwrite_existing'] ?? '0'; // 处理上传文件 if (!isset($_FILES['file'])) { echo json_encode(['success' => false, 'error' => '没有上传文件']); exit; } $file = $_FILES['file']; $original_filename = $file['name']; $temp_path = $file['tmp_name']; // 创建用户上传目录 $uploadDir = UPLOAD_PATH . $user_id . '/'; if (!file_exists($uploadDir)) { mkdir($uploadDir, 0755, true); } // 处理文件路径 $target_path = $uploadDir . $original_filename; $file_overwritten = false; // 检查文件是否已存在 if (file_exists($target_path)) { // 自动覆盖现有文件 if (unlink($target_path)) { $file_overwritten = true; } else { echo json_encode(['success' => false, 'error' => '无法删除旧文件,请检查文件权限']); exit; } } // 移动上传的文件 if (!move_uploaded_file($temp_path, $target_path)) { echo json_encode(['success' => false, 'error' => '文件移动失败']); exit; } // 这里继续您的数据库导入逻辑... // 假设您已经有了数据库导入的功能 $imported_rows = 0; // 这里应该是实际导入的行数 // 在成功响应中包含覆盖信息 echo json_encode([ 'success' => true, 'data' => [ 'rows_imported' => $imported_rows, 'file_size' => formatBytes(filesize($target_path)), 'upload_time' => date('Y-m-d H:i:s'), 'file_overwritten' => $file_overwritten ] ]); exit; } // 辅助函数:格式化字节大小 function formatBytes($bytes, $precision = 2) { $units = ['B', 'KB', 'MB', 'GB', 'TB']; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow); return round($bytes, $precision) . ' ' . $units[$pow]; }