Recursive Directory Listing (Show full directory structure)
This function will read the full structure of a directory. It's recursive becuase it doesn't stop with the one directory, it just keeps going through all of the directories in the folder you specify.
PHP Code:
<?php
function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "<strong>$spaces $file</strong><br />";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
echo "$spaces $file<br />";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
?>
Example Calls:
PHP Code:
getDirectory( "." );
// Get the current directory
getDirectory( "./files/includes" );
// Get contents of the "files/includes" folder
sorry but this doesn't work on my php server.. I NEED HELP lol.. if i want to show a directory /public_html/flash/ what do i have to put? and what is the "level" variable about??
thanx.
Peuplarchie, did you actually call the function or did you just copy/paste the code from above into your php page? Because you have to include the above code in <?PHP and ?> and then actually call it inside the php tags like so:
PHP Code:
<?
//Function code
//....
//Now call it by using one of these,
//depending on which function you chose above:
getdirectory('.');
//or
get_dir_iterative(/*etc.*/);
?>
Not being sniffy, should probably rewrite/repost myself: but the function is both getting the directory listing and printing the result, would be nice if it returned the data from the recursive directory scan (probably in an array) so programmer can specify how it is shown, either print or send to HTML template etc. keep functions doing one thing, getting data from filesystem or printing the result makes it more re-usable.
Not Sure if it's the best, and likely is not, but I modified the first function to get things working for now. This returns an array so that when i go back through the array, I can concatenate my keys with my values for use with something like fopen...
The getDirectory function is totally awesome! and exactly what i needed. here's the thing tho: when i was hacking it up on my dev box it was working great. but then, once i got it the way i wanted it, i uploaded the files to the web server, and now everything is coming up in random order.
is there a way to force it to read alphabetically? Is is my web server? is it something else entirely? anybody know of this issue?
The getDirectory function is totally awesome! and exactly what i needed. here's the thing tho: when i was hacking it up on my dev box it was working great. but then, once i got it the way i wanted it, i uploaded the files to the web server, and now everything is coming up in random order.
is there a way to force it to read alphabetically? Is is my web server? is it something else entirely? anybody know of this issue?
/* ** Put the following line into an index.php, ** with first line: include_once("dirLister.class.php"); ** Then erase the following line. */ $dirLister = new dirLister();
/* ** After running this once you will find two new files in the current directory: ** config.ini ** links.ini ** Once they exist, you might want to edit them, as needed, to get what you want. */
class dirLister { var $searchRootUrlPath; var $searchRootFilePath; var $currentDirUrlPath; var $currentDirFilePath; var $currentDisplayName=null; var $defaultDisplayName; var $linkTypes=array(); var $links=array(); var $allowedLinkTypesfile; var $config=array(); var $dbg=false;
function __construct() { $this->init(); $this->readLinkTypes(); $this->getLinks(); $this->getDefaultDisplay(); $this->show(); }
function init() { if(@stat("config.ini")) { /* A config.ini file does exist. You can edit this file, as needed, to search any permissable directory. */ $lines = file("config.ini"); foreach ($lines as $aline) { $tokens = explode("=",$aline); $label = trim($tokens[0]); $value = trim($tokens[1]); if(stristr($label, "path")){ if(strlen(strrchr($value,'/')) != 1){ $value .= '/'; } } $this->config[$label] = $value; } $this->searchRootUrlPath = $this->config['searchRootUrlPath']; $this->searchRootFilePath = $this->config['searchRootFilePath']; } else { if(strstr($_SERVER['SCRIPT_FILENAME'],"home")) { /* no config.ini file exists. If "home" appears in the path ** we'll make a desperate attempt to make this work. We'll assume ** the current path looks something like this (which it may not): ** /home/sandy/public_html/images/ */ $dirs = explode('/',dirname($_SERVER['SCRIPT_FILENAME'])); $tmpUrlPath='/~' . $dirs[2] ; $zapper = '/' . $dirs[1] . '/'. $dirs[2] . '/' . $dirs[3] . '/';
$tmp = ereg_replace($zapper,"", dirname($_SERVER['SCRIPT_FILENAME'])); $tmpUrlPath .= '/'.$tmp . '/'; $this->config['searchRootFilePath'] = dirname($_SERVER['SCRIPT_FILENAME']); $this->config['searchRootUrlPath'] = $tmpUrlPath; $this->searchRootFilePath = $this->config['searchRootFilePath']; $this->searchRootUrlPath = $this->config['searchRootUrlPath']; } else { /* ** No config.ini file exists. "home" does not appear in the path. ** So we'll assume we're in the global document root instead of a ~user/public_html directory ** This should work, regardless $_SERVER['DOCUMENT_ROOT'] */ $this->searchRootFilePath = dirname($_SERVER['SCRIPT_FILENAME']).'/'; $this->searchRootUrlPath = '/' . ereg_replace($_SERVER['DOCUMENT_ROOT'],"", $this->searchRootFilePath); } }