Displaying the Entire Contents of a Directory

The example shown here demonstrates how to display the entire contents of a directory by using the maptree() function to populate two arrays. One array, $directories, contains a list of directory names, while the $files array contains a list of files found in the directory that is being mapped. Two loops at the end of the example iterate through the two arrays and print their contents.

/* Displaying the entire contents of a directory */ require_once 'File/Find.php';

/* This example will recursively search through an entire

* directory and find all of the files/directories contained

* in the directory.

list($directories, $files) = File_Find::maptree('/tmp');

foreach ($directories as $directory) {

printf("Found directory: '%s'\n", $directory);

Because the script prints out the contents of the /tmp directory, your results will vary, but they'll look something like this:

Found directory:

Found directory:

Found directory:

Found directory:

Found directory:

Found file:

Found file:

Found file:

Found file:

Found file:

Found file:

Found file:

'/tmp' '/tmp/pear' '/tmp/pear/cache' '/tmp/hsperfdata_Nathan' '/tmp/503' '/tmp/cs_cache_lock_92' '/tmp/cups_noproof_log' '/tmp/objc_sharing_ppc_26' '/tmp/objc_sharing_ppc_4294967294' '/tmp/objc_sharing_ppc_502' '/tmp/objc_sharing_ppc_503' '/tmp/objc_sharing_ppc_92'

0 0

Post a comment

  • Receive news updates via email from this site