使用 PHP glob 获取文件夹 - 深度无限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5769514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get folders with PHP glob - unlimited levels deep
提问by Jens T?rnell
I have this working function that finds folders and creates an array.
我有这个工作功能,可以找到文件夹并创建一个数组。
function dua_get_files($path)
{
foreach (glob($path . "/*", GLOB_ONLYDIR) as $filename)
{
$dir_paths[] = $filename;
}
return $dir_paths;
}
This function can only find the directories on the current location. I want to find the directory paths in the child folders and their children and so on.
此功能只能查找当前位置上的目录。我想在子文件夹及其子文件夹中找到目录路径等等。
The array should still be a flat list of directory paths.
该数组仍应是目录路径的平面列表。
An example of how the output array should look like
输出数组的外观示例
$dir_path[0] = 'path/folder1';
$dir_path[1] = 'path/folder1/child_folder1';
$dir_path[2] = 'path/folder1/child_folder2';
$dir_path[3] = 'path/folder2';
$dir_path[4] = 'path/folder2/child_folder1';
$dir_path[5] = 'path/folder2/child_folder2';
$dir_path[6] = 'path/folder2/child_folder3';
回答by Pascal MARTIN
If you want to recursively work on directories, you should take a look at the RecursiveDirectoryIterator
.
如果你想递归地处理目录,你应该看看RecursiveDirectoryIterator
.
$path = realpath('/etc');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
echo "$name\n";
}
回答by Crusader
Very strange - everybody advice recursion, but better just cycle:
很奇怪 - 每个人都建议递归,但最好只是循环:
$dir ='/dir';
while($dirs = glob($dir . '/*', GLOB_ONLYDIR)) {
$dir .= '/*';
if(!$result) {
$result = $dirs;
} else {
$result = array_merge($result, $dirs);
}
}
回答by Alix Axel
Try this instead:
试试这个:
function dua_get_files($path)
{
$data = array();
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
if (is_dir($file) === true)
{
$data[] = strval($file);
}
}
return $data;
}
回答by DJafari
Use this function :
使用此功能:
function dua_get_files($path)
{
$dir_paths = array();
foreach (glob($path . "/*", GLOB_ONLYDIR) as $filename)
{
$dir_paths[] = $filename;
$a = glob("$filename/*", GLOB_ONLYDIR);
if( is_array( $a ) )
{
$b = dua_get_files( "$filename/*" );
foreach( $b as $c )
{
$dir_paths[] = $c;
}
}
}
return $dir_paths;
}
回答by crisc82
You can use php GLOB function, but you must create a recursive function to scan directories at infinite level depth. Then store results in a global variable.
您可以使用 php GLOB 函数,但您必须创建一个递归函数来扫描无限级深度的目录。然后将结果存储在全局变量中。
function dua_get_files($path) {
global $dir_paths; //global variable where to store the result
foreach ($path as $dir) { //loop the input
$dir_paths[] = $dir; //can use also "basename($dir)" or "realpath($dir)"
$subdir = glob($dir . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR); //use DIRECTORY_SEPARATOR to be OS independent
if (!empty($subdir)) { //if subdir is not empty make function recursive
dua_get_files($subdir); //execute the function again with current subdir
}
}
}
//usage:
$path = array('galleries'); //suport absolute or relative path. support one or multiple path
dua_get_files($path);
print('<pre>'.print_r($dir_paths,true).'</pre>'); //debug
回答by Beracah
For PHP, if you are on a linux/unix, you can also use backticks (shell execution) with the unix find
command. Directory searching on the filesystem can take a long time and hit a loop -- the system find
command is already built for speed and to handle filesystem loops. In other words, the system exec call is likely to cost far less cpu-time than using PHP itself to search the filesystem tree.
对于PHP,如果您使用的是 linux/unix,您还可以在 unixfind
命令中使用反引号(shell 执行)。文件系统上的目录搜索可能需要很长时间并遇到循环——系统find
命令已经为速度和处理文件系统循环而构建。换句话说,系统 exec 调用可能比使用 PHP 本身搜索文件系统树花费的 CPU 时间少得多。
$dirs = `find $path -type d`;
Remember to sanitize the $path input, so other users don't pass in security compromising path names (like from the url or something).
请记住清理 $path 输入,这样其他用户就不会传入危害安全的路径名(例如来自 url 或其他内容)。
To put it into an array
将其放入数组
$dirs = preg_split("/\s*\n+\s*/",`find $path -type d`,-1,PREG_SPLIT_NO_EMPTY);