You may be right nowBut recursive functions are a complex and messy thing, and most developers (including myself) don't really like to use them. That's why when my last project needed to scan a directory level for a single file (a typical recursive function task), I didn't even think about looping my own code. Instead, I used the pear and its file_find class directly, eliminating the pain of my search for multi-layered directory structures.
The File_find class is designed to allow you to actually do two things well. First, it allows you to scan a directory tree and then convert it into a PHP structure-a nested array-that reflects the parent-child relationship at the original level. Second, it allows you to search for one or more types of matching files under one directory tree. This type can be a simple string or an expression of a complex Perl rule, and File_find can handle either of these two formats, and will return an array that includes detailed file and path information for each pair.
To start, manually install the package, you can download it, and then extract its contents to the Pear root directory, or you can take advantage of the pear installer.
Next, create the following PHP script (List a) and save it in the root directory of your Web server's file:
PHP class Search Location directory tree List A
- < ? PHP
- Include class
- Include ("find.php");
- Initialize Finder
- $ Finder New file_find ();
- Read directory tree and print
- $ Tree = $finder- > maptree ("/tmp") ;
- Print_r ($tree);
- ?>
Here, I initialize a new File_find () object and call its Maptree () method with a directory path. The Maptree () method reads the specified directory and then produces its two-element hierarchy "array map". The first element in this array lists the subdirectories under all target directories found, and the second element lists all the files found. It is then possible to scan these sequences and use them in the application-for example, to rebuild a directory tree, or to filter files and directories based on custom criteria.
List B is an example of the above script output:
PHP class Search Locate directory tree List b
- Array
- (
- [0] => Array
- (
- [0] => / tmp
- [1] => /tmp/dummya
- [2] => /tmp/dummyb
- [3] => /tmp/dummya/dummyc
- )
- [1] => Array
- (
- [0] => /tmp/data.txt
- [1] => /tmp/dummyb/metoo.mp3
- [2] => /tmp/dummyb/track.dat
- [3] => /tmp/dummya/dummyc/parrot.gif
- )
- )
Alternatively, you can use the Maptreemultiple () method, which recursively reads the specified directory, producing a nested array that replicates its tree structure. The list C shows you an example.
PHP class Search Locate directory tree list C
- < ? PHP
- Include class
- Include ("find.php");
- Initialize Finder
- $ Finder New file_find ();
- Print Recursive directory tree
- $ Tree = $finder- > maptreemultiple ("/tmp") ;
- Print_r ($tree);
- ?>
List D shows the output:
PHP class Search Locate directory tree List D
- Array
- (
- [0] => data.txt
- [Dummya] = > Array
- (
- [DUMMYC] = > Array
- (
- [0] => parrot.gif
- )
- )
- [Dummyb] = > Array
- (
- [0] => metoo.mp3
- [1] => track.dat
- )
- )
Tip: You can add an optional parameter to Maptreemultiple () to limit the number of layers it queries down when performing recursion.
With the Glob () method, you can search for files in a specified directory that match a specific Perl-compatible rule expression. Take a look at the list E.
PHP class Search Locate directory tree List E
- < ? PHP
- Include class
- Include ("find.php");
- Initialize Finder
- $ Finder New file_find ();
- Search for matching files in named directory
- $ Results = $finder- > glob ("/mp3/i", "/usr/local/stuff", "Perl");
- Print_r ($results);
- ?>
Here, the Glob () method accesses the file with the string mp3 in all names in the specified directory. Notice my use of the modifier I, which makes the search not case-sensitive, and I used the third argument to tell the class to use the Perl regex function instead of the default PHP function.
The File_find class really works because of its search () method, which combines the ability of the Maptreemultiple () and Glob () methods to perform multi-level directory searches. The list f is an example.
PHP class Search Locating directory tree list F
- < ? PHP
- Include class
- Include ("find.php");
- Initialize Finder
- $ Finder New file_find ();
- Search for matching files
- In Named directory and subdirectories
- $ Results = $finder- > Search ("/exe/i", "/usr/local/winstuff", "Perl");
- Print_r ($results);
- ?>
The list G shows a sample of the output:
PHP class Search location directory tree List g
- Array
- (
- [0] => /usr/local/winstuff/4help. EXE
- [1] => /usr/local/winstuff/arj. EXE
- [2] => /usr/local/winstuff/bzip2.exe
- [3] => /usr/local/winstuff/crlf. EXE
- [4] => /usr/local/winstuff/decode. EXE
- [5] => /usr/local/winstuff/grep. EXE
- [6] => /usr/local/winstuff/gpg/gpg.exe
- [7] => /usr/local/winstuff/gpg/uninst-gnupg.exe
- [8] => /usr/local/winstuff/gpg/winpt.exe
- [9] => /usr/local/winstuff/fprot/f-prot. EXE
- )
This is your recursive directory query, just four lines of code!
As you can see, File_find makes it possible to simply and efficiently complete a relatively complex task, and you feel the least pressure, which is why I strongly recommend it to you. The next time you need to perform a file lookup on a PHP class search location tree, you might want to try it yourself. Wish you a happy programming!
http://www.bkjia.com/PHPjc/446283.html www.bkjia.com true http://www.bkjia.com/PHPjc/446283.html techarticle Everyone may be right now. But recursive functions are a complex and messy thing, and most developers (including myself) don't really like to use them. That's why when I ...