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 hereFeaturing 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!");
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More File Manipulation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
|
|
|
|
Continuous Integration (or CI) is a process that consists of continuously compiling, testing, inspecting, and deploying source code. In many Continuous Integration environments, this means running a new build anytime code within a source code management repository changes. The benefit of CI is simple: assembling software often greatly increases the likelihood that you will spot defects early, when they still are relatively manageable. In this tutorial, a companion to his series In pursuit of code quality, Andrew Glover introduces the fundamental aspects of Continuous Integration and steps you through how to set up a CI process using best-of-breed open source technologies. FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |