CoderZone.org

Category: >> PHP Code >> Recursive Directory Browser Bookmark and Share

<< lastnext >>

Snippet Name: Recursive Directory Browser

Description: This snippet recursively searches through the directory structure and returns all files in those directories. The file list is returned in an array.

If the file extension parameter is set, only files matching that extension will be displayed, ie, "jpg".

If the Display Full Path parameter is true, then the full path of the file will be returned. If it is false, only the file name will be returned. By default, this parameter is set to true.

Also see:
» Write or update an htpasswd file i...
» Active Directory - Reading the Use...
» Validate file extensions in upload...
» Javascript Include - allows dynami...
» Remove empty lines from a string o...
» Automatically find location of jav...
» Remove empty lines from text files
» Simple directory viewer / browser
» Delete a file in Visual Basic
» Copy a file in VB
» Reading and Writing to text files ...
» Parsing PHP in Javascript Files
» Perl version of file_get_contents&...
» Read the contents of an entire fil...
» Create an array from the lines of ...
» Remove Duplicate Lines from a File
» Extract Just The BODY Of An HTML F...
» Rename Files Interactively
» Sort A File List By Month Name In ...
» Get File Size Over HTTP
» Get A Directory Listing Of Specifi...
» Check If A Directory Allows File C...
» Copy Files By Date Modified
» Output To STDOUT Or A File
» Include All Files In A Directory
» More File Type Detection in PHP
» Get Current Directory Name
» Copy a remote file to your site
» Use htpasswd file with PHPscripts

Comment: (none)

Author:
Language: PHP
Highlight Mode: PHP
Last Modified: December 15th, 2010

/*
recursively searches through the directory 
structure and returns all files in those 
directories. The file list is returned in an array.
 
If the file extension parameter is set, only 
files matching that extension will be displayed, ie, "jpg"
 
If the Display Full Path parameter is true, 
then the full path of the file will be returned. 
If it is false, only the file name will be returned. 
By default, this parameter is set to true. 
*/
 
// directoryToArray(Path to Directory [,file extention [,Display Full Path]]
 
 
$main_dir = '/path/to/directory'; 
 
$flist = directoryToArray($main_dir);
 
print_r($flist);
 
 
function directoryToArray($directory, $extension="", $full_path = true) {
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				if (is_dir($directory. "/" . $file)) {
					$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $extension, $full_path)); 
				}
				else { 
					if(!$extension || (ereg("." . $extension, $file)))
					{
						if($full_path) {
							$array_items[] = $directory . "/" . $file;
						}
						else {
							$array_items[] = $file;
						}
					}
				}
			}
		}
		closedir($handle);
	}
	return $array_items;
}
 
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7