| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
A fairly simple script to list all directories and files in a base directory. Then allows users to navigate through the directories and sub-directories. You can find more scripts on my site here Featuring a fairly simple permission mode, you can set if the user can navigate above a given directory. You can also disable the whole script by changing $allow_access to 0. Note: Requires 2 images of your choice (A directory image and file image) By : andrew <? /* Author: Andrew Walsh Date: 23/07/2005 Cw-Username: Andrew */ //Config section $base_path = "/path/to/directory/"; //Set the base directory $above_base = 0; //Can the user navigate above the base directory or not? $directory_img = "./dir.gif"; $file_img = "./file.gif"; $allow_access = 1; //If false the script will not run... if($allow_access == 1){ if(!isset($_REQUEST['path'])){ //If no path set create a base path $path = substr($base_path, 0, -1); }else{ $path = $_REQUEST['path']; //If path is set in url then use that path } //Is the user trying to navigate above the base directory? if($above_base == 1){ //Is this option enabled? $b = explode("/", $base_path); //Split up the base path $c = count($b); //Count elements in the base path $c--; //Subtract last section of base path //Construct the path for the directory above the base directory/path for comparison later for($i=0; $i<$c; $i++){ $r = $b[$i].'/'; } //Is the request path above the base path? is the base_path not in the requested path? if($path == $r || strstr($path, $base_path) === FALSE){ //If yes then stop the script and output error message die("You are not allowed to navigate above the base path!"); //Output error }else{ $dir_handle = opendir($path) or die("Unable to open $path"); //Open the path } }else{ $dir_handle = opendir($path) or die("Unable to open $path"); //Open the path } //Generating the path to the directory above the current path.... $p = explode("/", $path); //Breakup the path into parts $c = count($p); //Count parts in the path $c--; //Subtract the ending section //Generate the path for($i=0; $i<$c; $i++){ $up_path .= $p[$i].'/'; } $up_path = substr($up_path, 0, -1); //Trim off the ending / //Display links to current path and directory above current directory echo ' <a href="?path='.$path.'"><b>.</b></a><br> <a href="?path='.$up_path.'"><b>..</b></a><br> '; while($f = readdir($dir_handle)){ //Create arrays of directories and files if(is_dir($f)){ //If file being read is a directory then store in the directory array $d[] = $f; //Add the link to the directories array }else{ $fs[] = $f; //Else its a file } } if(count($d) < 1){ }else{ sort($d); //Sort the directories array } if(count($fs) < 1){ }else{ sort($fs); //Sort the file array } foreach($d as $dir){ //Loop through directory array if($dir != "." && $dir != ".."){ //Remove the . and .. links as these are generated by the script //Echo out the directories with the directory image next to them.... echo '<a href="?path='.$path.'/'.$dir.'"><img src="'.$directory_img.'" border="0" /><b>'.$dir.'</b></a><br>'; } } foreach($fs as $f){ //Loop through the files array //Display the links to the files with the file image before.... echo '<img src="'.$file_img.'" border="0" />'.$f.'<br>'; } closedir($dir_handle); //Close the current working directory.... }else{ die("You are not allowed to access this script!"); } ?>
More File Manipulation Code Articles |
| |
| |