Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

View all images in a directory, sorted by date

Status
Not open for further replies.

jobi

Technical User
Jun 30, 2000
24
NO
I need a script that views all images in a directory, and because the directory contains a lot of images, I want the newest image to be presented first.

(I'm not very familiar with cgi, I read but don't write the code...)

I've found a simple script that views the images, but they are sorted alfabetically.

Is there a simple way to modify the script to present the newest image first, or do you know a better way to do this?

Here's the script:

=====================================
#!/usr/bin/perl

# CHANGE LINE ABOVE IF NECESSARY TO LOCATION OF YOUR PERL INTERPRETER.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# WebImageLister 1.0
# --------------------
# A no-frills VERY simple but VERY effective, easy-to-install Perl
# script to display .GIF, .JPG, .JPEG, .PNG and .BMP images in a table
# as clickable thumbnails from a single web directory.
#
# Defaults: Thumbnails -- 80 pixels wide. Table width -- 7 columns.
#
# IMPORTANT: Lines in ALL CAPS indicate where changes can or *must* be made
# to customize script for your system and/or preferences.
#
# Script must be run from a cgi-bin directory on the web server.
#
# Source of script: #
# Adapted by Henry Baker from a generic script found somewhere on the Web.
# Website: # Email: hbaker@ipa.net
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$pos=0;
$upname="";
$count=0;

# YOU *MUST* CHANGE THIS URL *BETWEEN* THE QUOTE MARKS TO YOUR IMAGE DIRECTORY.
# BE SURE TO INCLUDE THE TRAILING SLASH.

$imgpath="
# UNIX SYSTEM PATH (DEFAULT)
# ----------------------------
# YOU *MUST* CHANGE THE PATH BELOW *BETWEEN* THE QUOTE MARKS (the ALL CAPS STUFF)
# TO POINT TO YOUR IMAGE DIRECTORY.

opendir(curdir,"/home/website/ open Directory!");

# NT SYSTEM PATH (CHANGE FOR AN NT SERVER)
# ------------------------------------------
# FOR AN NT SYSTEM CHANGE THIS PATH *BETWEEN* THE QUOTE MARKS TO POINT TO
# YOUR IMAGE DIRECTORY. REMOVE THE COMMENT POUND SIGN (#) BEFORE IT AND
# PLACE A COMMENT POUND SIGN BEFORE THE UNIX SYSTEM PATH ABOVE.

# opendir(CURDIR,"E:\\ open Directory!");

@names=readdir(curdir);
print "content-type: text/html", "\n\n";

# CHANGE TEXT BETWEEN TITLE TAGS TO REFLECT YOUR HTML PAGE TITLE:

print &quot;<html><head><title>Image Directory Demo</title></head>\n&quot;;

print &quot;<body bgcolor=black><center><table bgcolor='#cecece' border='0' cellpadding=2 cellspacing=0 width='100%'>\n&quot;;

# CHANGE TEXT IN NEXT LINE TO HEADING FOR YOUR IMAGE DIRECTORY:

print &quot;<center><font face=verdana, arial, san serif size=+2 color=white><b>WebImageLister Images Directory Demo</b></font>\n
<br><font face='Verdana, Arial, san serif' SIZE=-1 color=white><b>(Click images to open in new window)</b></font></center></br>&quot;;

# FOR LARGER OR SMALLER THUMBNAILS, CHANGE PIXEL WIDTH NUMBERS IN NEXT FIVE ARRAYS:

for $name(@names)
{
if ($count==0)
{
print &quot;<tr>\n&quot;;
}
$upname=uc($name);
$pos=index($upname,&quot;.GIF&quot;);
if ($pos > 0)
{
print &quot;<td align='center'><a href='&quot;.$imgpath.$name.&quot;' target=_blank><img src='&quot;.$imgpath.$name.&quot;' width=80></a><br><font face='verdana, arial, san serif' size=-1>$name</font></td>\n&quot;;
$count=$count+1;
}
$pos=index($upname,&quot;.JPG&quot;);
if ($pos > 0)
{
print &quot;<td align='center'><a href='&quot;.$imgpath.$name.&quot;' target=_blank><img src='&quot;.$imgpath.$name.&quot;' width=80></a><br><font face='verdana, arial, san serif' size=-1>$name</font></td>\n&quot;;
$count=$count+1;
}
$pos=index($upname,&quot;.JPEG&quot;);
if ($pos > 0)
{
print &quot;<td align='center'><a href='&quot;.$imgpath.$name.&quot;' target=_blank><img src='&quot;.$imgpath.$name.&quot;' width=80></a><br><font face='verdana, arial, san serif' size=-1>$name</font></td>\n&quot;;
$count=$count+1;
}
$pos=index($upname,&quot;.PNG&quot;);
if ($pos > 0)
{
print &quot;<td align='center'><a href='&quot;.$imgpath.$name.&quot;' target=_blank><img src='&quot;.$imgpath.$name.&quot;' width=80></a><br><font face='verdana, arial, san serif' size=-1>$name</font></td>\n&quot;;
$count=$count+1;
}
$pos=index($upname,&quot;.BMP&quot;);
if ($pos > 0)
{
print &quot;<td align='center'><a href='&quot;.$imgpath.$name.&quot;' target=_blank><img src='&quot;.$imgpath.$name.&quot;' width=80></a><br><font face='verdana, arial, san serif' size=-1>$name</font></td>\n&quot;;
$count=$count+1;
}

# CHANGE THE NUMBER IN THE NEXT LINE TO HOW MANY COLUMNS YOU WANT IN IMAGE TABLE:

if ($count==7)
{
print &quot;</tr>\n&quot;;
$count=0;
}
}
closedir(curdir);
if ($count>0)
{
print &quot;</tr>\n&quot;;
}
print &quot;</table></center></body></html>\n&quot;;

==================================
 
Once you have your @names array you can build a hash like this(its called a Schwartzian Transform) :

my %NAMEHASH ;
foreach my $filename(@names){
$NAMEHASH{ $filename } = -M $filename ;
}
#
# The above creates a hash keyed on filename
# but that has a value of the files AGE
#

my @temp = sort { $NAMEHASH{ $a } <=> $NAMEHASH{ $b } } keys %NAMEHASH ;
# The above sorts this new hash on the VALUES.

foreach my $sortedfile(@temp){
print &quot;$sortedfile\n&quot; ;
}
# The above prints the filenames in the order they came out of the sort operation in, date descending!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top