either drop into an image directory as index.php
// -OR- point to script in Apache DirectoryIndex directive:
// DirectoryIndex index.html index.htm /cgi-bin/image-list.php
/*
image-list.php -- fancy image indexing using PHP
Copyright (C) 2003 by Chris Snyder (csnyder@chxo.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
function debug() {
$args = func_get_args();
print "
";
foreach( $args AS $arg ) {
print htmlentities( print_r( $arg, 1), ENT_QUOTES, 'utf-8' );
}
exit( "
" );
}
$thumbwidth=80;
$thumbheight=60;
$thumblimit= 800; // (Max filesize in KB for displaying thumbnail)
$skip= $_GET["skip"];
$perpage= $_GET["perpage"];
$showsource= $_GET["showsource"];
$REQUEST_URI= $_SERVER['REQUEST_URI'];
$DOCUMENT_ROOT= $_SERVER['DOCUMENT_ROOT'];
$SCRIPT_NAME= $_SERVER['SCRIPT_NAME'];
$SCRIPT_FILENAME= $_SERVER['SCRIPT_FILENAME'];
$SERVER_SIGNATURE= $_SERVER['SERVER_SIGNATURE'];
//debug($_SERVER);
if ($skip=="") $skip= 0;
if ($perpage=="") $perpage= 48;
function myfilesize($file) {
// First check if the file exists.
if(!is_file($file)) exit("File does not exist!");
// Setup some common file size measurements.
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
// Get the file size in bytes.
$size = filesize($file);
if($size < $kb) return $size." B";
else if($size < $mb) return round($size/$kb,2)." KB";
else if($size < $gb) return round($size/$mb,2)." MB";
else if($size < $tb) return round($size/$gb,2)." GB";
else return round($size/$tb,2)." TB";
}
if (substr($REQUEST_URI,-1)=="/") {
$length= strlen($REQUEST_URI)-1;
$uripath= substr($REQUEST_URI,0,$length);
}
else {
$uripath= dirname($REQUEST_URI);
}
$filepath= $DOCUMENT_ROOT.urldecode($uripath);
// Some users have suggested:
// $filepath= realpath(".");
// instead of using $DOCUMENT_ROOT.
//print "filepath= $filepath and realpath=".realpath(".")." and uripath= $uripath ";
//debug($_SERVER);
$lastslash= strrpos($uripath,"/");
$parenturi= substr($uripath,0,$lastslash+1);
if ($parenturi=="") $parenturi= "/";
$displayuripath= urldecode($uripath);
// show source code?
if ($showsource) $sourceinfo= "| Script: $SCRIPT_FILENAME ( source code )";
else $sourceinfo= "| show source code";
print "
$displayuripath
Image index of $displayuripath
Up to parent folder | Jump to files $sourceinfo
";
if ($skip) {
$prevskip= $skip - $perpage;
if ($prevskip<0) $prevskip= 0;
$prevtag= "Previous Page";
print "$prevtag
";
}
if ($handle = opendir($filepath)) {
print "
Icon |
Name |
Filesize |
Timestamp |
";
// FIRST PASS for subdirectories only -- makes $mydirarray, which can be sorted alphabetically
$count= 0;
while ($file = readdir($handle)) {
if (substr($file,0,1)==".") continue;
if ($file=="index.php") continue;
if (@is_dir("$filepath/$file")) {
$mydirarray[$count]="$file";
$count= $count+1;
}
}
closedir($handle);
// now sort and print subfolders...
if (is_array($mydirarray)) {
sort($mydirarray);
reset($mydirarray);
foreach ($mydirarray AS $key=>$file) {
$timestamp= filemtime("$filepath/$file");
$modified= date("r", $timestamp);
if ($evenrow) {
$evenrow=0;
$rowclass= "evenrow";
}
else {
$evenrow=1;
$rowclass= "oddrow";
}
if ($key==($count-1)) $separator= " ";
$safefile= urlencode($file);
print "
|
$file/$separator |
- |
$modified |
";
}
}
// SECOND PASS for files only
$handle = opendir($filepath);
$skipped= 0;
$index= 0;
$count= 0;
while ($file = readdir($handle)) {
// don't show .dotfiles, self, directories, or links (links are bad??? hmm)
if (substr($file,0,1)==".") continue;
if ($file=="index.php") continue;
if (@is_dir("$filepath/$file")) continue;
if (@is_link("$filepath/$file")) continue;
$myfilearray[$count]= $file;
$count= $count+1;
}
closedir($handle);
// now sort on filename
if (is_array($myfilearray)) {
sort($myfilearray);
reset($myfilearray);
foreach ($myfilearray AS $key=>$file) {
if ($index >= $perpage) {
$shownext= 1;
break;
}
if ($skipped<$skip && $skip!= 0) {
$skipped= $skipped + 1;
continue;
}
$index= $index + 1;
$timestamp= filemtime("$filepath/$file");
$modified= date("r", $timestamp);
if ($evenrow) {
$evenrow=0;
$rowclass= "evenrow";
}
else {
$evenrow=1;
$rowclass= "oddrow";
}
unset($info);
unset($extrainfo);
unset($sizetag);
$filesize= myfilesize("$filepath/$file");
if ($imagesize=@getimagesize("$filepath/$file",$info)) {
$width= $imagesize[0];
$height= $imagesize[1];
if ($width>$thumbwidth) {
$ratio= $thumbwidth / $width;
$width=$thumbwidth;
$height= $height*$ratio;
}
if ($height>$thumbheight) {
$ratio= $thumbheight / $height;
$height=$thumbheight;
$width= $width*$ratio;
}
switch ($imagesize[2]) {
case 5: $icontag= ""; break;
case 7: $icontag= ""; break;
case 8: $icontag= ""; break;
default: $icontag= ""; break;
}
$sizetag= "($imagesize[0]x$imagesize[1])";
if (isset ($info['APP13'])) {
$iptc = iptcparse ($info['APP13']);
if (is_array($iptc)) {
foreach ($iptc AS $key=>$val) {
if ($key=="2#000") continue;
$extrainfo.="$val[0]
";
}
}
}
// don't show thumbnails bigger than 500K!
if (filesize("$filepath/$file")>($thumblimit*1024)) $icontag= "";
}
else {
// try to determine from extension...
$lastdot= strrpos($file, ".");
$extension= strtolower(substr($file,$lastdot+1));
//flerror("File extension is $extension.");
switch ($extension) {
case "pdf": $icontag= ""; break;
case "txt": $icontag= ""; break;
case "htm": $icontag= ""; break;
case "html": $icontag= ""; break;
case "wav": $icontag= ""; break;
case "au": $icontag= ""; break;
case "aiff": $icontag= ""; break;
case "mp3": $icontag= ""; break;
case "ogg": $icontag= ""; break;
case "mov": $icontag= ""; break;
case "avi": $icontag= ""; break;
case "mpg": $icontag= ""; break;
case "mpeg": $icontag= ""; break;
case "mp4": $icontag= ""; break;
case "divx": $icontag= ""; break;
case "zip": $icontag= ""; break;
default: $icontag= ""; break;
}
}
// file row
$safefile= urlencode($file);
print "
$icontag |
$file $sizetag |
$filesize |
$modified |
";
}
}
else print "
No files here. |
";
print "
Image |
Name |
Filesize |
Timestamp |
";
if ($shownext) {
$nextskip= $skip + $index;
if ($prevtag) $prevtag .= " | ";
print "$prevtag Next Page
";
}
elseif ($skip) {
print "$prevtag
";
}
print "
Up to parent folder
";
}
if ($showsource) {
print "
PHP Source:
";
$void= show_source($SCRIPT_FILENAME);
}
print "
image-list.php Copyright (C) 2003 by Chris Snyder
This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome
to redistribute it under certain conditions; please refer to the
GNU General Public License for details.
";
?>