0) { $message = "✅ 成功删除 {$deleted_count} 个项目"; if ($failed_count > 0) $message .= ",{$failed_count} 个失败"; } else { $message = "❌ 删除失败,请检查权限。"; $message_type = 'error'; } $current_path = isset($_POST['current_path']) ? $_POST['current_path'] : $current_path; $current_path = realpath($current_path); } // 处理压缩操作 elseif (isset($_POST['compress']) && isset($_POST['selected_items'])) { $selected_items = $_POST['selected_items']; if (empty($selected_items)) { $message = "❌ 请至少选择一个文件或文件夹。"; $message_type = 'error'; } else { $tar_name = isset($_POST['tar_name']) && trim($_POST['tar_name']) != '' ? trim($_POST['tar_name']) : 'archive_' . date('Ymd_His'); if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $tar_name)) { $message = "❌ 压缩包名称只能包含字母、数字、下划线、减号和点。"; $message_type = 'error'; } else { $tar_path = $current_path . DIRECTORY_SEPARATOR . $tar_name . '.tar.gz'; $result = createTarGz($selected_items, $tar_path, $current_path); if ($result === true) { $message = "✅ 成功创建压缩包: " . htmlspecialchars($tar_name . '.tar.gz'); $message_type = 'success'; } else { $message = "❌ 压缩失败:\n" . $result; $message_type = 'error'; } } } $current_path = isset($_POST['current_path']) ? $_POST['current_path'] : $current_path; $current_path = realpath($current_path); } // 处理重命名操作 elseif (isset($_POST['rename']) && isset($_POST['target']) && isset($_POST['new_name'])) { $target = realpath($_POST['target']); $new_name = trim($_POST['new_name']); if ($target && isPathAllowed($target)) { if (empty($new_name) || preg_match('#[\\/\\0]#', $new_name) || $new_name == '.' || $new_name == '..') { $message = "❌ 无效的名称"; $message_type = 'error'; } else { $parent_dir = dirname($target); $new_path = $parent_dir . DIRECTORY_SEPARATOR . $new_name; if (!isPathAllowed($new_path)) { $message = "❌ 新路径不在允许范围内"; $message_type = 'error'; } elseif (file_exists($new_path)) { $message = "❌ 目标文件/目录已存在: " . htmlspecialchars($new_name); $message_type = 'error'; } elseif (rename($target, $new_path)) { $message = "✅ 重命名成功: " . htmlspecialchars(basename($target)) . " → " . htmlspecialchars($new_name); $message_type = 'success'; // 如果重命名的是当前目录,更新当前路径到父目录 if ($target == $current_path) { $current_path = $parent_dir; $message .= " 当前工作目录已被重命名,已切换到上级目录。"; } } else { $message = "❌ 重命名失败,请检查权限。"; $message_type = 'error'; } } } else { $message = "❌ 无效的目标路径或不在允许范围内。"; $message_type = 'error'; } $current_path = isset($_POST['current_path']) ? $_POST['current_path'] : $current_path; $current_path = realpath($current_path); } // 修改权限或时间 else { $target = isset($_POST['target']) ? $_POST['target'] : ''; $target = realpath($target); if ($target && isPathAllowed($target)) { if (isset($_POST['chmod']) && isset($_POST['permissions'])) { $perms = octdec($_POST['permissions']); if (chmod($target, $perms)) { $message = "✅ 成功修改权限: " . substr(sprintf('%o', $perms), -4); } else { $message = "❌ 修改权限失败。可能原因:PHP运行用户不是文件所有者。"; $message_type = 'error'; } } elseif (isset($_POST['touch']) && isset($_POST['mtime'])) { $mtime = strtotime($_POST['mtime']); if ($mtime !== false && touch($target, $mtime)) { $message = "✅ 成功修改修改时间为: " . date('Y-m-d H:i:s', $mtime); } else { $message = "❌ 修改时间失败,时间格式错误或无权限。"; $message_type = 'error'; } } } else { $message = "❌ 无效的路径或不在允许范围内。"; $message_type = 'error'; } $current_path = isset($_POST['current_path']) ? $_POST['current_path'] : $current_path; $current_path = realpath($current_path); } if ($current_path === false || !isPathAllowed($current_path) || !is_dir($current_path)) { $current_path = WEB_ROOT; } } // 获取文件列表 $items = []; if (is_dir($current_path) && is_readable($current_path)) { $dir = opendir($current_path); while (($file = readdir($dir)) !== false) { if ($file == '.' || $file == '..') continue; $full_path = $current_path . DIRECTORY_SEPARATOR . $file; $items[] = [ 'name' => $file, 'path' => $full_path, 'is_dir' => is_dir($full_path), 'size' => is_file($full_path) ? filesize($full_path) : getDirectorySize($full_path), 'perms' => fileperms($full_path), 'mtime' => filemtime($full_path), 'is_readable' => is_readable($full_path), 'is_writable' => is_writable($full_path), 'extension' => pathinfo($file, PATHINFO_EXTENSION) ]; } closedir($dir); usort($items, function($a, $b) { if ($a['is_dir'] == $b['is_dir']) { return strnatcasecmp($a['name'], $b['name']); } return $a['is_dir'] ? -1 : 1; }); } // 辅助函数 function getUploadErrorMessage($error_code) { switch ($error_code) { case UPLOAD_ERR_INI_SIZE: return "文件超过服务器限制"; case UPLOAD_ERR_FORM_SIZE: return "文件超过表单限制"; case UPLOAD_ERR_PARTIAL: return "文件只有部分被上传"; case UPLOAD_ERR_NO_FILE: return "没有文件被上传"; case UPLOAD_ERR_NO_TMP_DIR: return "找不到临时文件夹"; case UPLOAD_ERR_CANT_WRITE: return "文件写入失败"; case UPLOAD_ERR_EXTENSION: return "文件上传被扩展阻止"; default: return "未知错误"; } } function permToString($perms) { if (($perms & 0xC000) == 0xC000) $info = 's'; elseif (($perms & 0xA000) == 0xA000) $info = 'l'; elseif (($perms & 0x8000) == 0x8000) $info = '-'; elseif (($perms & 0x6000) == 0x6000) $info = 'b'; elseif (($perms & 0x4000) == 0x4000) $info = 'd'; elseif (($perms & 0x2000) == 0x2000) $info = 'c'; elseif (($perms & 0x1000) == 0x1000) $info = 'p'; else $info = 'u'; $info .= (($perms & 0x0100) ? 'r' : '-'); $info .= (($perms & 0x0080) ? 'w' : '-'); $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-')); $info .= (($perms & 0x0020) ? 'r' : '-'); $info .= (($perms & 0x0010) ? 'w' : '-'); $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-')); $info .= (($perms & 0x0004) ? 'r' : '-'); $info .= (($perms & 0x0002) ? 'w' : '-'); $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-')); return $info; } function formatSize($size) { if ($size === '-') return '-'; $units = ['B', 'KB', 'MB', 'GB', 'TB']; $i = 0; while ($size >= 1024 && $i < count($units)-1) { $size /= 1024; $i++; } return round($size, 2) . ' ' . $units[$i]; } function getDirectorySize($dir) { $size = 0; // 为提升性能,此处暂时注释递归计算,有需要可取消注释 return $size; } function deleteDirectory($dir) { if (!is_dir($dir)) return false; $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . DIRECTORY_SEPARATOR . $file; if (is_dir($path)) { deleteDirectory($path); } else { unlink($path); } } return rmdir($dir); } function createTarGz($items, $tar_path, $current_dir) { $debug_info = []; $debug_info[] = "========== 压缩调试信息 =========="; $debug_info[] = "当前目录: " . $current_dir; $debug_info[] = "目标路径: " . $tar_path; if (!function_exists('shell_exec')) { return "shell_exec 函数被禁用,无法执行 tar 命令。"; } $debug_info[] = "shell_exec 函数可用"; $tar_check = shell_exec('which tar 2>&1'); $debug_info[] = "tar 命令检查: " . ($tar_check ? trim($tar_check) : "未找到"); if (empty(trim($tar_check))) { return "系统不支持 tar 命令。\n\n" . implode("\n", $debug_info); } $target_dir = dirname($tar_path); $debug_info[] = "目标目录可写: " . (is_writable($target_dir) ? "是" : "否"); if (!is_writable($target_dir)) { return "目标目录不可写: " . $target_dir . "\n\n" . implode("\n", $debug_info); } $valid_items = []; foreach ($items as $item) { $item_path = realpath($item); if ($item_path && file_exists($item_path)) { $valid_items[] = $item_path; $debug_info[] = "有效项目: " . $item_path; } else { $debug_info[] = "无效项目: " . $item; } } if (empty($valid_items)) { return "没有找到有效的文件或目录。\n\n" . implode("\n", $debug_info); } $items_for_tar = []; foreach ($valid_items as $item) { $items_for_tar[] = escapeshellarg(basename($item)); } $tar_basename = basename($tar_path); $items_str = implode(' ', $items_for_tar); $command = "cd " . escapeshellarg($current_dir) . " 2>&1 && tar -czf " . escapeshellarg($tar_basename) . " " . $items_str . " 2>&1"; $debug_info[] = "执行命令: " . $command; $output = []; $return_var = 0; exec($command, $output, $return_var); $debug_info[] = "返回码: " . $return_var; if (!empty($output)) { $debug_info[] = "命令输出:\n" . implode("\n", $output); } if ($return_var === 0 && file_exists($tar_path)) { $file_size = filesize($tar_path); $debug_info[] = "压缩成功,大小: " . formatSize($file_size); return true; } else { $debug_info[] = "================================="; $error_msg = "压缩失败 (返回码: $return_var)\n\n"; if (!empty($output)) { $error_msg .= "错误详情:\n" . implode("\n", $output) . "\n\n"; } $error_msg .= "完整调试信息:\n" . implode("\n", $debug_info); return $error_msg; } } // 编辑器界面(支持任何文件编辑) if (isset($_GET['edit']) && !$edit_error && $edit_content !== '') { ?>