创建目录中所有文件的嵌套数组的代码PHP 的子目录不起作用

发布于 2024-09-14 14:44:52 字数 2551 浏览 4 评论 0原文

我正在尝试使用 PHP 手册网站(由用户发布)上的示例函数。该函数应该返回服务器指定目录中所有文件和/或文件夹的嵌套数组。

我刚刚学习php,所以我认为更有可能是我做错了什么,而不太可能是该功能不起作用。但如果有比下面的函数更简单的方法,请告诉我。

我能够迭代并回显基本路径中的所有文件,但不能迭代和回显示例子文件夹中的文件。子文件夹所在的索引要么是空白,要么只是文件夹名称而不是数组,具体取决于我使用的参数。当我能够获取文件夹名称时,我使用 is_array 对其进行了测试,结果返回 false。

函数如下:

list_array:返回一个数组,可选地包含文件系统路径中的所有文件、仅目录或仅文件

  • @param $base_path 字符串,绝对或相对路径

  • @param $filter_dir boolean 从结果中过滤目录(如果 $recursive 为 true,则忽略除最后一个目录之外的目录)

  • @param $filter_files boolean 从结果中过滤文件

  • @param $exclude string 始终忽略的管道分隔文件字符串

  • @param $recursive boolean 将目录下降到底部?

  • @return $result_list array 嵌套数组或 false

    函数directory_list($directory_base_path, $filter_dir = false, $filter_files = false, $exclude = ".|..|.DS_Store|.svn", $recursive = true){
    $directory_base_path = rtrim($directory_base_path, "/") 。 “/”;
    
    if (!is_dir($directory_base_path)){
        error_log(__FUNCTION__ .“文件位于:$directory_base_path 不是目录。”);
        返回假;
    }
    
    $结果列表=数组();
    $exclude_array = 爆炸("|", $exclude);
    
    if (!$folder_handle = opendir($directory_base_path)) {
        error_log(__FUNCTION__ 。“无法打开目录:$directory_base_path”);
        返回假;
    }别的{
        while(false !== ($filename = readdir($folder_handle))) {
            if(!in_array($filename, $exclude_array)) {
                if(is_dir($directory_base_path . $filename . "/")) {
                    if($recursive && strcmp($filename, ".")!=0 && strcmp($filename, "..")!=0 ){ // 防止无限递归
                        error_log($directory_base_path . $filename . "/");
                        $result_list[$filename] = Directory_list("$directory_base_path$filename/", $filter_dir, $filter_files, $exclude, $recursive);
                    }elseif(!$filter_dir){
                        $结果列表[] = $文件名;
                    }
                }elseif(!$filter_files){
                    $结果列表[] = $文件名;
                }
            }
        }
        关闭($folder_handle);
        返回$结果列表;
    }
    

    }

这是我尝试过的众多方法之一。该数组包含 5 个元素,但仅显示其中 4 个。回显 $array[4] 为空。我现在只是想获取嵌套数组。但如果有一种更简单的方法来迭代它,任何提示都会非常感激。

$directory_base_path = "../sandbox/";
$filter_dir = false;
$filter_files = false;
$exclude_files = ".|..|.DS_Store|dir_read_test.php";
$recursive = true;
$array = directory_list($directory_base_path, $filter_dir, $filter_files, $exclude_files, $recursive);
$array_length = count($array);
echo $array_length;
echo "<br/>";
$count = 0;

while ( $array_length >= $count ) {
 echo $array[$count];
 echo "<br/>";
 $count = $count + 1;
}

I'm attempting to use a sample function on the PHP manual website (posted by a user). The function is supposed to return a nested array of all of the files and/or folders in a specified directory of your server.

I'm just learning php, so I think it's more likely that I'm doing something wrong, and less likely that the function doesn't work. But if there is an easier method than the function below please let me know.

I'm able to iterate through and echo all of the files in the base path, but not the files in my sample sub-folder. The index where the sub-folder should be is either blank or just the folder name and not an array depending on the parameters I use. When I was able to get the folder name, I tested it with is_array which returned false.

Here is the function:

list_array: return an array containing optionally all files, only directories or only files at a file system path

  • @param $base_path string either absolute or relative path

  • @param $filter_dir boolean Filter directories from result (ignored except in last directory if $recursive is true)

  • @param $filter_files boolean Filter files from result

  • @param $exclude string Pipe delimited string of files to always ignore

  • @param $recursive boolean Descend directory to the bottom?

  • @return $result_list array Nested array or false

    function directory_list($directory_base_path, $filter_dir = false, $filter_files = false, $exclude = ".|..|.DS_Store|.svn", $recursive = true){
    $directory_base_path = rtrim($directory_base_path, "/") . "/";
    
    if (!is_dir($directory_base_path)){
        error_log(__FUNCTION__ . "File at: $directory_base_path is not a directory.");
        return false;
    }
    
    $result_list = array();
    $exclude_array = explode("|", $exclude);
    
    if (!$folder_handle = opendir($directory_base_path)) {
        error_log(__FUNCTION__ . "Could not open directory at: $directory_base_path");
        return false;
    }else{
        while(false !== ($filename = readdir($folder_handle))) {
            if(!in_array($filename, $exclude_array)) {
                if(is_dir($directory_base_path . $filename . "/")) {
                    if($recursive && strcmp($filename, ".")!=0 && strcmp($filename, "..")!=0 ){ // prevent infinite recursion
                        error_log($directory_base_path . $filename . "/");
                        $result_list[$filename] = directory_list("$directory_base_path$filename/", $filter_dir, $filter_files, $exclude, $recursive);
                    }elseif(!$filter_dir){
                        $result_list[] = $filename;
                    }
                }elseif(!$filter_files){
                    $result_list[] = $filename;
                }
            }
        }
        closedir($folder_handle);
        return $result_list;
    }
    

    }

Here is one of the many things I've tried. The array contains 5 elements, but only 4 of them display. Echoing $array[4] is blank. I'm just trying to get the nested array at this point. But if there is an easier way to iterate through it, any tips are much appreciated.

$directory_base_path = "../sandbox/";
$filter_dir = false;
$filter_files = false;
$exclude_files = ".|..|.DS_Store|dir_read_test.php";
$recursive = true;
$array = directory_list($directory_base_path, $filter_dir, $filter_files, $exclude_files, $recursive);
$array_length = count($array);
echo $array_length;
echo "<br/>";
$count = 0;

while ( $array_length >= $count ) {
 echo $array[$count];
 echo "<br/>";
 $count = $count + 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-09-21 14:44:52

仔细看看这个例程:

    $result_list[$filename] = directory_list(/*recursive*/);
 else
    $result_list[] = $filename;

有什么区别?递归条目放置在索引 $filename 下,而直接内容则使用 [] 放置在升序整数索引下。您的循环将仅显示整数索引。

如果您愿意,

print_r($array);

您将看到所有条目。 (当然,您也可以将 [$filename] 替换为 []。)要自己循环遍历它们,请将 while 循环替换为

foreach ( $array as $k=>$v )
{
    echo "$k: $v<br/>";
}

(对于子目录,这将显示“subdir: Array”,但这是另一个主题;您可以测试 is_array($v) 并据此采取一些操作)

Have a close look at that routine:

    $result_list[$filename] = directory_list(/*recursive*/);
 else
    $result_list[] = $filename;

What's the difference? The recursive entries are put under index $filename, while the direct contents are put under an ascending integer index, using []. Your loop will only show the integer indexes.

If you would

print_r($array);

you will see all entries. (Or you could replace [$filename] with [], of course.) To loop through them yourself, replace the while loop with

foreach ( $array as $k=>$v )
{
    echo "$k: $v<br/>";
}

(for the subdir, this will show "subdir: Array", but that's another topic; you could test is_array($v) and take some action based on that)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文