7.6 Loading All Files in a Directory into an Array


You want to load all the files in a directory into an array for easy processing and manipulation.

Technique

Use the maptree() method in PEAR's File_Find class:

 <?php include_once 'File/Find.php'; list ($directories, $files) = File_Find::maptree('/www/htdocs'); ?> 

Comments

To load an entire directory tree into an array, we use PEAR's File_Find class. The File_Find class provides many useful methods for loading and searching directory trees. The maptree() method will return an array containing two arrays: The first array is a list of all the directories in the specified directory, and the second is a list of all the files in all the directories.

If you want to load only the current directory, use the mapdir() method. It has the same syntax as the maptree() method, except that it returns an array of all the file and directory names in the specified directory:

 <?php include_once 'File/Find.php'; $fsystem = File_Find::mapdir ('/www/htdocs'); foreach ($fsystem as $ent) {     print "$ent\n"; } ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net