Very simple files/sub-dirs/images lister.
Images get a small thumbnail, and sub-dirs are supported too.
By : orioriginal
<?php
/*
Files Lister
============
Very simple files/sub-dirs/media lister.
images get a small thumbnail, and sub-dirs are supported too.
no need to put the file in every sub-dir, just the most upper one.
you can assign a relative home-dir, from which the user cannot list out.
you can cancel the show sub-dirs option.
you can show only media files if you want.
support for up-dir (..) is cancelled for security reasons.
when a file named icon.gif/jpg/ico exists in a sub-dir - it is used as a thumbnail for the sub-dir.
Installation:
Just put the file in your home dir, and name it what you want.
Options:
parameter method options default description
========= ====== ======= ======= ===========
dir $_GET $dirdef subdir (without a slash)
media $_GET 1/0 $mediadef hide non-media files
$homedir variable the base relative home dir. u must add a slash at the end.
$mediaopt variable 1/0 1 show the option to switch between hide/show non-media files?
$mediadef variable 1/0 0 media default status, and permanent status when $mediaopt==0
$showdirs variable 1/0 1 show sub-dirs? when disabled, the dir parameter is also not available
author: Original (aka OriA)
date: 2005-07-30
copyrights: common, it took me 3 minutes of base code (and 2 hours of improovments...)
feel free to do whatever you want with it :)
Thats me, Original
*/
//error_reporting(0);
// parameters:
$homedir=''; // relative home dir
$mediadef=0; // hide non-medias by default?
$mediaopt=1; // show the option of switching between media mode?
$showdirs=1; // show sub-dirs?
// link to me
$me=basename($_SERVER['PHP_SELF']);
// current dir
if ($showdirs){
echo "Content of ";
// get sub-dir from GET
$cdir=''.$_GET['dir'];
// security - dont allow get out of home-dir by deleting suspicious dir characters
$cdir=str_replace('\\' ,'/',$cdir);
while (strpos($cdir,'/..')!==false) $cdir=str_replace('/..','/',$cdir);
while (strpos($cdir,'/./')!==false) $cdir=str_replace('/./','/',$cdir);
while (strpos($cdir,'//' )!==false) $cdir=str_replace('//' ,'/',$cdir);
while (strpos('\\/.',substr($cdir,0,1))!==false) $cdir=substr($cdir,1);
echo "$cdir/ ";
}else{
echo "Content:";
$cdir=$dir='';
}
$dir=$homedir.$cdir;
if ($dir!='') $dir.='/';
// show only media files? (media = php mediatypes = medias and swf)
if ($mediaopt){
$media=''.$_GET['media'];
if ($media==='') $media=$mediadef;
if ($media){
echo " <a href=\"$me?dir=$cdir&media=0\">(show all files)</a>";
}else{
echo " <a href=\"$me?dir=$cdir&media=1\">(hide non-media)</a>";
}
}else{
$media=$mediadef;
}
echo "<br>";
// fix for different code pages
function fixurl($str){
//return str_replace('%'.dechex(ord('/')),'/',htmlentities(rawurlencode($str)));
$ret='';
for($i=0;$i<strlen($str);$i++){
if ($str{$i}=='/'){
$ret.='/';
}else{
//$ret.='%'.dechex(ord($str{$i}));
$ret.=htmlentities(rawurlencode($str{$i}));
}
}
return $ret;
}
// show files
if (!$files=glob($dir."*.*")){
echo "<i>no files</i><br>";
}else{
foreach ($files as $file){
if (!is_dir($file) && $file!=basename($_SERVER['PHP_SELF'])){
// thumbnail:
// once there was a cool exif thumbnail system here - but the simple extensions method prooved to be better...
// get file extension
$ext=pathinfo($file);
$ext=strtolower($ext['extension']);
$images=array('jpg','jpeg','jp2','jpx','gif','png','ico','bmp','wbmp','tiff','iff','tif','pcx');
$movies=array('avi','mov','swf','swc');
$type='';
if (in_array($ext,$images)) $type='image';
if (in_array($ext,$movies)) $type='movie';
$text=$file;
if ($type=='movie'){
$text.=" <embed align=middle width=36 height=36 border=0 autostart=false src=\"".fixurl($dir).fixurl(basename($file))."\"></embed>";
//$text.=" <img align=middle height=36 border=0 src=\"http://www.w3.org/Icons/movie.gif\" />";
}elseif ($type=='image'){
$text.=" <img align=middle width=36 height=36 border=0 src=\"".fixurl($dir).fixurl(basename($file))."\" />";
}
if ($type || !$media){
echo "<a href=\"".fixurl($dir).fixurl(basename($file))."\">$text</a><br>";
}
}
}
}
// show sub-dirs
if ($showdirs){
if ($mediaopt){
$img="&media=$media";
}else{
$img='';
}
$d=0;
$dh = opendir($dir.'.');
while (false !== ($file = readdir($dh))) {
if ($file!='..' && $file!='.' && is_dir($dir.$file)) {
if ($d++==0) echo "<br>Sub-dirs:<br>";
$text=$file;
// add an icon to a sub-folder
if (file_exists($file.'/icon.gif')) $text.=" <img border=1 height=36 width=36 src=\"".fixurl($dir).fixurl($file)."/icon.gif\" />";
if (file_exists($file.'/icon.jpg')) $text.=" <img border=1 height=36 width=36 src=\"".fixurl($dir).fixurl($file)."/icon.jpg\" />";
if (file_exists($file.'/icon.jpeg'))$text.=" <img border=1 height=36 width=36 src=\"".fixurl($dir).fixurl($file)."/icon.jpeg\" />";
if (file_exists($file.'/icon.ico')) $text.=" <img border=1 height=36 width=36 src=\"".fixurl($dir).fixurl($file)."/icon.ico\" />";
echo "<b><a href=\"$me?dir=".$cdir.($cdir=='' ? '':'/').fixurl(basename($file))."$img\">$text</a><br></b>";
}
}
closedir($dh);
// show home link
if ($cdir!='') echo "<br><a href=\"$me?dir=$img\">Back to home dir</a><br>";
}
?>
| 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 Site Navigation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
This whitepaper presents the benefits of successfully introducing static analysis into your organization using IBM Rational Software Analyzer. Additionally, it identifies some common pitfalls that can hinder the effective use of static analysis tooling as well as presents 10 simple strategies designed to help you quickly realize the value of static analysis using Rational Software Analyzer. FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
|
|
|
|
Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server. FREE! Go There Now!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. 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! |