一個簡單的 PHP 腳本,通過表單輸入路徑,列出服務器指定目錄下的所有文件與子目錄,區分顯示顏色,並統計總項目數。
<html>
<head><title>查看服務器目錄</title></head>
<body>
<form method="post" action="">
輸入目錄名: <input type="text" name="InputDirRoot">
<input type="submit" value="查看">
</form>
<hr>
<?php
$n = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['InputDirRoot'])) {
$dir = $_POST['InputDirRoot'];
$handle = opendir($dir);
echo "目錄列表:<font color=#FF0000>$dir</font><br><hr>";
while ($file = readdir($handle)) {
$n++;
if (is_dir($dir . "/" . $file))
$file = "<font color=#006600>[$file]</font>";
else
$file = "<font color=#993300>$file</font>";
echo " $file<br>";
}
closedir($handle);
}
echo "<hr>總共 $n 項.";
?>
</body></html>
php.ini 中 register_globals = On(舊版本特性,現代 PHP 建議用 $_POST)/ 列根目錄,/php 列 php 目錄