如何使用jQuery或PHP
获取名称中包含Pages
的文件夹的直接路径
预期输出类似于:../../First_Folder/First_Folder_Pages
或../../Second_Folder/Second_Folder_Pages
答案 0 :(得分:1)
此功能足以用于文件列表。只需对它进行ajax调用,json_encode在发送回javascript之前输出
function scan($dir)
{
$output = array();
$handle = opendir($dir);
if ($handle != false) {
while ($file = readdir($handle)) {
if ($file == '.' || $file == '..')
continue;
$path = $dir . '/' . $file;
if (is_dir($path))
$output += scan($path);
else
$output[] = $path;
}
}
return $output;
}
如果你想将文件夹添加到数组中,只需更改if语句,如;
if (is_dir($path))
$output += scan($path);
$output[] = $path;
您也可以使用RecursiveDirectoryIterator类:stackoverflow
<强>更新强>
如果您想在输出中添加文件夹,只需删除“else”关键字。
如果您调用函数;
scan('/root/');
输出会给你带来的结果;
'/root/First_Folder/First_Folder_file1',
'/root/Second_Folder/Second_Folder_file1',
'/root/Second_Folder/Second_Folder_file2'
但如果你删除“else”关键字,你会得到如下结果;
'/root/First_Folder',
'/root/First_Folder/First_Folder_file1',
'/root/Second_Folder',
'/root/Second_Folder/Second_Folder_file1',
'/root/Second_Folder/Second_Folder_file2'