[英]Php Browsing Multiple Directories
使用上一篇文章中的代碼顯示目錄
<?php
$files = array();
$dir = opendir('races/ob/'); // open the cwd..also do an err check.
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != "..") and ($file != "index.php")) {
$files[] = $file; // put in array.
}
}
natsort($files); // sort.
// print.
foreach($files as $file) {
echo("<span class='txt-spacing'><a href='$file'>$file</a> <br />\n</>");
}
?>
我的問題是如何使生成的鏈接轉到服務器上的正確文件路徑,然后在新頁面中打開該頁面,並顯示所有文件列表(.txt文件),供用戶單擊和查看數據。
我的目錄結構是這個
Races
|
OldBird
|
Clubs Name Listing
|
List all .txt files for users to view
function scandir_recursive($path)
{
if ($result = scandir($path))
{
$scan = $result = array_filter($result, function ($a) { return !in_array($a, ['.', '..', 'index.php']); });
foreach ($scan as $sub)
if (is_dir($subdir = "$path/$sub"))
if ($dir = scandir_recursive($subdir))
$result = array_merge($result, array_map(function ($a) use ($sub) { return "$sub/$a"; }, $dir));
}
return $result;
}
$files = scandir_recursive('races/ob');
natsort($files);
foreach ($files as $file)
echo("<a class='txt-spacing' href='$file'>$file</a><br>\n");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.