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! |
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. 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!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |