The specific implementation way of the PHP class search location Tree _php Tutorial

Source: Internet
Author: User
Tags glob pear php class
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

 
  
  
  1. < ? PHP
  2. Include class
  3. Include ("find.php");
  4. Initialize Finder
  5. $ Finder New file_find ();
  6. Read directory tree and print
  7. $ Tree = $finder- > maptree ("/tmp") ;
  8. Print_r ($tree);
  9. ?>

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

 
 
  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [0] => / tmp
  6. [1] => /tmp/dummya
  7. [2] => /tmp/dummyb
  8. [3] => /tmp/dummya/dummyc
  9. )
  10. [1] => Array
  11. (
  12. [0] => /tmp/data.txt
  13. [1] => /tmp/dummyb/metoo.mp3
  14. [2] => /tmp/dummyb/track.dat
  15. [3] => /tmp/dummya/dummyc/parrot.gif
  16. )
  17. )

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

 
  
  
  1. < ? PHP
  2. Include class
  3. Include ("find.php");
  4. Initialize Finder
  5. $ Finder New file_find ();
  6. Print Recursive directory tree
  7. $ Tree = $finder- > maptreemultiple ("/tmp") ;
  8. Print_r ($tree);
  9. ?>

List D shows the output:

PHP class Search Locate directory tree List D

 
 
    1. Array
    2. (
    3. [0] => data.txt
    4. [Dummya] = > Array
    5. (
    6. [DUMMYC] = > Array
    7. (
    8. [0] => parrot.gif
    9. )
    10. )
    11. [Dummyb] = > Array
    12. (
    13. [0] => metoo.mp3
    14. [1] => track.dat
    15. )
    16. )

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

 
  
  
  1. < ? PHP
  2. Include class
  3. Include ("find.php");
  4. Initialize Finder
  5. $ Finder New file_find ();
  6. Search for matching files in named directory
  7. $ Results = $finder- > glob ("/mp3/i", "/usr/local/stuff", "Perl");
  8. Print_r ($results);
  9. ?>

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

 
  
  
  1. < ? PHP
  2. Include class
  3. Include ("find.php");
  4. Initialize Finder
  5. $ Finder New file_find ();
  6. Search for matching files
  7. In Named directory and subdirectories
  8. $ Results = $finder- > Search ("/exe/i", "/usr/local/winstuff", "Perl");
  9. Print_r ($results);
  10. ?>

The list G shows a sample of the output:

PHP class Search location directory tree List g

 
 
  1. Array
  2. (
  3. [0] => /usr/local/winstuff/4help. EXE
  4. [1] => /usr/local/winstuff/arj. EXE
  5. [2] => /usr/local/winstuff/bzip2.exe
  6. [3] => /usr/local/winstuff/crlf. EXE
  7. [4] => /usr/local/winstuff/decode. EXE
  8. [5] => /usr/local/winstuff/grep. EXE
  9. [6] => /usr/local/winstuff/gpg/gpg.exe
  10. [7] => /usr/local/winstuff/gpg/uninst-gnupg.exe
  11. [8] => /usr/local/winstuff/gpg/winpt.exe
  12. [9] => /usr/local/winstuff/fprot/f-prot. EXE
  13. )

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.