简体   繁体   中英

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. 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'); in my script, which makes the memory limit to 1GB but I am not able to solve the issue.

scandir is returning . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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