ls)index.php)<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Private File Box</title>
<style>body{font-family:sans-serif;max-width:800px;margin:2rem auto;padding:0 1rem;} a{display:block;margin:.5rem 0;}</style>
</head>
<body>
<h2>Private File Box</h2>
<?php
function listFiles(): void {
// 安全:指定絕對路徑,避免命令注入
$dir = __DIR__;
$cmd = escapeshellcmd("ls -1 " . escapeshellarg($dir));
exec($cmd, $files, $code);
if ($code !== 0) {
echo "無法讀取目錄
";
return;
}
foreach ($files as $file) {
if ($file === 'index.php') continue; // 隱藏自身
$url = rawurlencode($file); // 空格→%20,中文→%E4%B8%AD...
$name = htmlspecialchars($file, ENT_QUOTES, 'UTF-8');
echo "<a href=\"$url\" download>$name</a>";
}
}
listFiles();
?>
</body>
</html>
| 函數 | 用途 |
|---|---|
escapeshellcmd() | 轉義 Shell 元字元,防命令注入 |
escapeshellarg() | 將參數包裹單引號,安全傳遞路徑 |
rawurlencode() | RFC 3986 編碼,空格編為 %20(非 +) |
htmlspecialchars() | 防 XSS,輸出安全 HTML |
download 屬性 | HTML5 強制下載而非在瀏覽器開啟 |
scandir() / FilesystemIterator 純 PHP 實作,跨平台、無 Shell 依賴。realpath() 驗證在根目錄內)。