Share: PHP file traversal methods
/**
- * Obtain all files in the current directory and subdirectory.
- * @ Param string $ dir path name
- * @ Return array path array of all files
- */
- Function get_files1 ($ dir ){
- $ Files = array ();
If (! Is_dir ($ dir )){
- Return $ files;
- }
$ Handle = opendir ($ dir );
- If ($ handle ){
- While (false! ==( $ File = readdir ($ handle ))){
- If ($ file! = '.' & $ File! = '..'){
- $ Filename = $ dir. "/". $ file;
- If (is_file ($ filename )){
- $ Files [] = $ filename;
- } Else {
- $ Files = array_merge ($ files, get_files ($ filename ));
- }
- }
- } // End while
- Closedir ($ handle );
- }
- Return $ files;
- } // End function
Method 2: Use the globglob () function to search for all file paths that match the pattern according to the rules used by the libc glob () function, similar to the rules used by common shells. Do not perform abbreviation extension or parameter substitution. Returns an array containing matching files/directories. If an error occurs, FALSE is returned. This function cannot act on remote files. the checked files must be accessed through the file system of the server. This function is used to search for files in a directory. Example:
/**
- * Get all files in the current directory
- * @ Param string $ dir path name
- * @ Return array path array of all files
- */
- Function get_files ($ dir ){
- $ Dir = realpath ($ dir )."/";
- $ Files = array ();
If (! Is_dir ($ dir )){
- Return $ files;
- }
$ Pattern = $ dir ."*";
- $ File_arr = glob ($ pattern );
Foreach ($ file_arr as $ file ){
- If (is_dir ($ file )){
- $ Temp = get_files ($ file );
If (is_array ($ temp )){
- $ Files = array_merge ($ files, $ temp );
- }
- } Else {
- $ Files [] = $ file;
- } // End if
- }
- Return $ files;
- } // End function
- ?>
Method 3: Use the directory class counterfeit object-oriented mechanism to read a directory. The dir () function opens a directory handle and returns an object. This object contains three methods: read (), rewind (), and close (). Two attributes are available. The handle attribute can be used in other Directory functions, such as readdir (), rewinddir (), and closedir. The path attribute is set to the opened directory path. If the operation succeeds, the function returns a directory stream. otherwise, false and an error are returned. You can add "@" before the function name to hide the error output. Note: the order of directory items returned by the read method depends on the system. Note: This function defines the internal class Directory, meaning that you cannot use the same name to define your own class. Example:
/**
- * Recursively display all files in the specified directory
- * Use the dir function
- * @ Param string $ dir directory address
- * @ Return array $ files file list
- * @ Site bbs.it-home.org
- */
- Function get_files ($ dir ){
- $ Files = array ();
If (! Is_dir ($ dir )){
- Return $ files;
- }
$ D = dir ($ dir );
- While (false! ==( $ File = $ d-> read ())){
- If ($ file! = '.' & $ File! = '..'){
- $ Filename = $ dir. "/". $ file;
If (is_file ($ filename )){
- $ Files [] = $ filename;
- } Else {
- $ Files = array_merge ($ files, get_files ($ filename ));
- }
- }
- }
- $ D-> close ();
- Return $ files;
- }
Method 4: Use the RecursiveDirectoryIterator class. this method is effective from PHP 5.0. Example:
/**
- * Use RecursiveDirectoryIterator to traverse files and list all file paths
- * @ Param RecursiveDirectoryIterator $ dir specifies the directory's RecursiveDirectoryIterator instance
- * @ Return array $ files file list
- */
- Function get_files ($ dir ){
- $ Files = array ();
For (; $ dir-> valid (); $ dir-> next ()){
- If ($ dir-> isDir ()&&! $ Dir-> isDot ()){
- If ($ dir-> haschildren ()){
- $ Files = array_merge ($ files, get_files ($ dir-> getChildren ()));
- };
- } Else if ($ dir-> isFile ()){
- $ Files [] = $ dir-> getPathName ();
- }
- }
- Return $ files;
- }
$ Path = "/var/www ";
- $ Dir = new RecursiveDirectoryIterator ($ path );
- Print_r (get_files ($ dir ));
|