简体   繁体   English

数组大小在PHP scandir()中受到限制

[英]Array size getting limited in PHP scandir()

I'm using a recursive function to return a list of all the image files in a particular directory. 我正在使用递归函数返回特定目录中所有图像文件的列表。 The function works fine until a specific size of the returned array is achieved (what I assume being the issue). 该函数可以正常工作,直到实现返回数组的特定大小为止(我认为是问题所在)。 Is there anything else I can do to solve the issue, or an alternate that can be implemented here without the code being changed much as the application is live? 我还有什么其他方法可以解决该问题,或者可以在不执行大量更改代码的情况下在此处实施替代方法? Here is my recursive function. 这是我的递归函数。

function scanDirectories($rootDir, $allowext, $allData=array()) {
        $dirContent = scandir($rootDir);
            foreach($dirContent as $key => $content) {
                $path = $rootDir.'/'.$content;
                $ext = substr($content, strrpos($content, '.') + 1);

                if(in_array($ext, $allowext)) {
                    if(is_file($path) && is_readable($path)) {
                        $allData[] = $path;
                    }elseif(is_dir($path) && is_readable($path)) {
                        // recursive callback to open new directory
                        $allData = scanDirectories($path, $allData);
                    }
                }
            }
            return $allData;
        }

For example I have a directory having 83 image files, but I am able to return list of only 28 of them. 例如,我有一个包含83个图像文件的目录,但是我只能返回其中28个图像的列表。 On searching over the internet, I found that this could be a result of memory_limit which I can increase using ini_set('memory_limit', '1024M'); 在互联网上搜索时,我发现这可能是memory_limit的结果,可以使用ini_set('memory_limit', '1024M');来增加它ini_set('memory_limit', '1024M'); in my script, which makes the memory limit to 1GB but I am not able to solve the issue. 在我的脚本中,这将内存限制为1GB,但我无法解决问题。

scandir is returning . scandir正在返回. and .. (current and upper directory) as a list of directory. .. (当前目录和上一级目录)作为目录列表。 and it may cause your script to run forever with infinity recursive. 可能会导致您的脚本以无限递归永久运行。

Try replace the line 尝试更换线

}elseif(is_dir($path) && is_readable($path)) {

with

}elseif(is_dir($path) && is_readable($path) && $content!='.' && $content!='..' ) {

may fix your problem. 可能会解决您的问题。

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

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