<?php
/**
* 文件目录树查看器
* 用于快速扫描并展示网站根目录下所有文件和文件夹
* 使用后请及时删除
*/
// ===== 简单安全防护(可选) =====
// 允许的IP地址(留空则允许所有)
$allowed_ips = []; // 例如 ['192.168.1.100', '127.0.0.1'];
if (!empty($allowed_ips) && !in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
die('访问被拒绝');
}
// 简单密码验证(如果需要)
$auth_password = ''; // 设置密码则启用,留空则不启用
if (!empty($auth_password) && (!isset($_POST['pass']) || $_POST['pass'] !== $auth_password)) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
die('密码错误');
}
echo '<form method="post"><input type="password" name="pass"><button>提交</button></form>';
exit;
}
// ===== 配置 =====
// 若 ROOT_PATH 已定义则使用,否则使用 __DIR__(脚本所在的目录)
$root = defined('ROOT_PATH') ? ROOT_PATH : __DIR__;
$root = realpath($root) ?: __DIR__;
// 要排除的目录(相对路径,相对于根目录)
$exclude_dirs = [
'cache',
'temp',
'vendor',
'node_modules',
'.git',
'downloads',
];
// ===== 递归构建树 =====
function buildTree($dir, $exclude = [], $prefix = '') {
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
if (!is_dir($dir)) return '';
$items = scandir($dir);
$items = array_diff($items, ['.', '..']);
// 过滤排除的目录
$filtered = [];
foreach ($items as $item) {
$fullPath = $dir . DIRECTORY_SEPARATOR . $item;
$relative = str_replace($GLOBALS['root'], '', $fullPath);
$relative = ltrim($relative, DIRECTORY_SEPARATOR);
$exclude_flag = false;
foreach ($exclude as $ex) {
if (strpos($relative, $ex) === 0) {
$exclude_flag = true;
break;
}
}
if (!$exclude_flag) {
$filtered[] = $item;
}
}
// 按字母排序,目录优先
usort($filtered, function($a, $b) use ($dir) {
$isDirA = is_dir($dir . DIRECTORY_SEPARATOR . $a);
$isDirB = is_dir($dir . DIRECTORY_SEPARATOR . $b);
if ($isDirA && !$isDirB) return -1;
if (!$isDirA && $isDirB) return 1;
return strcasecmp($a, $b);
});
$output = '';
$total = count($filtered);
$i = 0;
foreach ($filtered as $item) {
$i++;
$isLast = ($i == $total);
$fullPath = $dir . DIRECTORY_SEPARATOR . $item;
$prefixChar = ($isLast) ? '└── ' : '├── ';
$childPrefix = ($isLast) ? ' ' : '│ ';
$output .= $prefix . $prefixChar . $item;
if (is_dir($fullPath)) {
$output .= DIRECTORY_SEPARATOR;
}
$output .= "\n";
if (is_dir($fullPath) && !in_array(basename($fullPath), ['.', '..'])) {
$output .= buildTree($fullPath, $exclude, $prefix . $childPrefix);
}
}
return $output;
}
// ===== 生成树形文本 =====
$tree = buildTree($root, $exclude_dirs);
$tree = rtrim($tree, "\n");
$tree = $root . "\n" . $tree;
// ===== HTML输出 =====
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件目录树</title>
<style>
body { font-family: monospace; padding: 20px; background: #f5f7fa; }
.container { max-width: 1200px; margin: 0 auto; background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
pre { background: #1e2a3a; color: #f0f4f8; padding: 16px; border-radius: 6px; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.toolbar { margin-bottom: 16px; }
.btn { padding: 6px 16px; background: #1abc9c; color: #fff; border: none; border-radius: 4px; cursor: pointer; }
.btn:hover { background: #16a085; }
.info { font-size: 14px; color: #555; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="container">
<h2>📁 文件目录树</h2>
<div class="info">
根目录:<code><?php echo htmlspecialchars($root); ?></code><br>
排除目录:<code><?php echo htmlspecialchars(implode(', ', $exclude_dirs)); ?></code>
</div>
<div class="toolbar">
<button class="btn" onclick="downloadTree()">📥 下载树形文本</button>
</div>
<pre id="treeContent"><?php echo htmlspecialchars($tree); ?></pre>
</div>
<script>
function downloadTree() {
const text = document.getElementById('treeContent').textContent;
const blob = new Blob([text], {type: 'text/plain'});
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'file_tree.txt';
a.click();
URL.revokeObjectURL(a.href);
}
</script>
</body>
</html> 目录树脚本
摘要: 运行后自动生成目录树,可在服务器上运行
发表评论 (0)
留下你的足迹