Several methods of php folder traversal

小云云
Release: 2023-03-20 15:30:02
Original
1546 people have browsed it

This article mainly shares with you several methods of php folder traversal, hoping to help everyone.

Function

function dirTree(){
   if(!is_dir($path)) return [];        $files = [];        $dir = opendir($path);        while($file = readdir($dir)) {            if($file == '.' || $file == '..') continue;            $new_path = trim($path, '/').'/'.trim($file, '/');            $files[] = $new_path;            if(is_dir($new_path)){                $files = array_merge($files, $this->ergodicDir2($new_path));
           }
       }
       closedir($dir);        return $files;
}
Copy after login

Directory Class

function dirTree(){
   if(!is_dir($path)) return [];        $files = [];        $dir_h = dir($path);        while($file = $dir_h->read()){            if($file == '.' || $file == '..') continue;            $new_path = trim($path, '/').'/'.trim($file, '/');            $files[] = $new_path;            if(is_dir($new_path)){                $files = array_merge($files, $this->ergodicDir3($new_path));
           }
       }        $dir_h->close();        return $files;
}
Copy after login

Iterator

function dirTree(){
   if(!is_dir($path)) return [];    $files = [];        $dir = new \DirectoryIterator($path);        foreach ($dir as $key => $file){            if($file->getFilename() == '.' || $file->getFilename() == '..'){                continue;
           }            $files[] = $file->getPathname();            if($file->isDir()){                $files = array_merge($files, $this->ergodicDir5($file->getPathname()));
           }
       }        return $files;
}
Copy after login

Related recommendations:

PHP folder traversal, image compression at equal proportions

The above is the detailed content of Several methods of php folder traversal. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!