简体   繁体   English

PHP案例切换行为打破了递归scandir函数

[英]PHP case switch behaviour breaks a recursive scandir function

during my quest for some basic php knowledge , I tried to do a simple function to read files and dirs that will not use recursive iterators. 在我寻求一些基本的PHP知识时,我试图做一个简单的函数来读取不会使用递归迭代器的文件和目录。 (more of an exercise but actually needed for any php<5.3 servers) (更多的练习但实际上需要任何php <5.3服务器)

the function works great - but when i tried to add a switch ($output) - it no longer lists all the files (for example, before adding the switch, it can see 61 files in 4 folders (3+root) but with the added switch it can only see 47 (only root) . am I doing something wrong here ? why does the switch statement break the faux "recursive-nest" ?? 该功能运行良好 - 但当我尝试添加一个switch ($输出) - 它不再列出所有文件(例如,在添加开关之前,它可以看到4个文件夹中的61个文件(3 +根)但是添加switch它只能看到47(只有root)。我在这里做错了吗?为什么switch语句打破了虚假的“recursive-nest”?

    function o99_list_my_files_scandir($dir,$output) 
    { 

        $result = $result2 = array(); 

        $root = scandir($dir); 

        foreach($root as $value) 
        { 

          if($value === '.' || $value === '..' || $value == 'Thumb.db' || $value == 'Thumbs.db' || $value == '') { continue;} 

            if(is_file("$dir/$value")) {$result[]="$dir/$value"; continue;} // array files
            if(is_dir("$dir/$value")) {$result2[]="$dir/$value"; continue;} // array dirs

            foreach(o99_list_my_files_scandir("$dir/$value") as $value) 

            { 
                $result[] = $value; 
            }
        } 
            // this is the troublemaker ...

            switch ($output) {
                case 'dirs' :
                return array_filter($result2);  // clean empty results of dirs
                break;

                case 'files' :
                return array_filter($result);   // Clean empty array of files
                break; 
            }

} 

is there any other simple way to get the function to return separate lists of dirs AND files and not a unified array ? 有没有其他简单的方法来让函数返回dirs AND文件的单独列表而不是统一的数组?

Because in this line... 因为在这一行......

foreach(o99_list_my_files_scandir("$dir/$value") as $value)

...you don't give $output to the recursively called function, so it won't output anything. ...你没有给递归调用函数的$output ,所以它不会输出任何东西。 Change it to: 将其更改为:

foreach(o99_list_my_files_scandir("$dir/$value", $output) as $value)

Actually you should have gotten a warning about the missing argument. 实际上你应该对失踪的论点发出警告。

You do not seem to set $output anywhere. 你似乎没有在任何地方设置$output

BTW: For learning purposes I strongly recommend adding error_reporting(E_ALL); 顺便说一句:出于学习目的,我强烈建议添加error_reporting(E_ALL); as very first line of your script and re-run (first, without fixing anything), because you should have seen a warning as 2nd function parameter is not optional. 作为脚本的第一行并重新运行(首先,不修复任何内容),因为您应该看到警告,因为第二个函数参数不是可选的。 You got signature 你有签名

function o99_list_my_files_scandir($dir,$output) 

but call 但是打电话

o99_list_my_files_scandir("$dir/$value")

while you should 而你应该

o99_list_my_files_scandir("$dir/$value", $output)

in your foreach() 在你的foreach()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM