the function of this script, is to distinguish wether the returned array is a file or directory, if it is a file, it generates a link to download the file, otherwise if it's a dir, it lists the files/dirs in that directory, works on windows _and_ *nix platforms, useful for mp3 sites... (also easy configuration)
By : phpie
<?
/*
* the function of this script, is to distinguish wether the returned array is a file or directory,
* if it is a file, it generates a link to download the file, otherwise if it's a dir,
* it lists the files/dirs in that directory, works on windows _and_ *nix platforms, useful for mp3 sites...
* you could also stream content with this too :)
*
* v1.0: - initial release
* v1.1: - small fix for null directory
* v1.2: - small security fix
* v1.3: - changed some stuff
*
* PHP4+ is required for foreach();
* permission granted to modify, redistribute the script, whatever, do what you want...
* -- don@progeny.org.za
*/
/* config for the script */
$download_path = "/path/to/your/files"; /* path to your files, NB: no slash at the end */
$sort = "asort"; /* array sorting - alphabetical sorting for the array */
/* start the script... no more editing from here on... */
/* get a list of the files + dirs and turn the list into an array */
function file_list($dir) {
global $sort;
global $file_file_count;
if (is_dir($dir)) {
$fd = @opendir($dir);
while (($part = @readdir($fd)) == true) {
clearstatcache();
if ($part != "." && $part != "..") {
$file_array[] = $part;
}
}
if ($fd == true) {
closedir($fd);
}
if (is_array($file_array)) {
$sort($file_array);
$file_file_count = count($file_array);
return $file_array;
} else {
return false;
}
} else {
return false;
}
}
/* function to convert to Mb, Kb and bytes */
function file_size($size) {
$megabyte = 1024 * 1024;
if ($size > $megabyte) { /* literal.float */
$re_sized = sprintf("%01.2f", $size / $megabyte) . " Mb";
} elseif ($size > 1024) {
$re_sized = sprintf("%01.2f", $size / 1024) . " Kb";
} else {
$re_sized = $size . " bytes";
}
return $re_sized;
}
/* get a list of the files/dirs, put them into a table. */
function generate_file_list($path) {
global $download_path;
global $PHP_SELF;
$final_path = str_replace("//","/",str_replace("..","",urldecode($path)));
$file_array = file_list("$download_path/$final_path/");
echo "<b>$final_path/</b>\n";
echo "<br><br>\n\n";
if ($file_array == false) { /* check if the dir is an array before we process it to foreach(); */
echo "directory empty\n";
} else {
echo "<table width=\"75%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
echo "<tr><td><b>file</b></td><td><b>size</b></td></tr>\n";
foreach ($file_array as $file_name) {
$is_file = "$download_path/$final_path/$file_name";
$final_dir_name = urlencode($final_path); /* urlencode(); to prevent any broken links - decode on do_download(); */
$final_file_name = urlencode($file_name);
$file_size = filesize("$download_path/$final_path/$file_name");
$final_file_size = file_size($file_size);
if (is_file($is_file)) {
print "<tr><td><a href=\"$PHP_SELF?go=download&path=$final_dir_name&file=$final_file_name\">$file_name</a></td><td>$final_file_size</td></tr>\n";
} elseif (is_dir($is_file)) {
print "<tr><td><a href=\"$PHP_SELF?go=list&path=$final_dir_name/$final_file_name\">$file_name/</a></td><td><dir></td></tr>\n"; /* we don't need a size for a directory */
}
}
echo "</table>\n";
}
}
/* allow the user to download the file... */
function do_download($path,$file) {
global $download_path;
$get_path = str_replace("//","/",str_replace("..","",stripslashes(urldecode($path)))); /* fopen adds \ to ' - so we strip 'em. */
$get_file = str_replace("//","/",str_replace("..","",stripslashes(urldecode($file))));
header("Content-Disposition: atachment; filename=$get_file");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize("$download_path/$get_path/$get_file"));
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen("$download_path/$get_path/$get_file","r");
print fread($fp,filesize("$download_path/$get_path/$get_file"));
fclose($fp);
exit;
}
if (!isset($go)) {
$go = "dirlist";
} if ($go == "dirlist") {
generate_file_list(""); /* null, so we get a list for the root directory */
} elseif ($go == "list" && isset($path)) {
if (isset($path)) { /* if the path is null - it returns a list for the root directory */
generate_file_list($path); /* get a list for the path specified */
} else {
generate_file_list("");
}
} elseif ($go == "download") {
if (isset($path) && isset($file)) {
do_download($path,$file); /* download the file... */
} else {
echo "no file selected to download :)\n";
}
}
?>
| 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! |
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |