home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 March / PCWK0397.iso / novell / webserv3 / wstkb1.exe / SCRIPTS / PERL / ALBUM.PL
Perl Script  |  1996-12-10  |  3KB  |  94 lines

  1. #!/usr/bin/perl
  2. # The above line is a throw back to my Unix days...ignore it.
  3. #
  4. # Description:
  5. #
  6. # A Perl script to create an HTML photo album of .GIF. and .JPG files.  It
  7. # specifically looks for all graphic files in the directory specified on the
  8. # URL and lists them and includes the text from corresponding .TXT files.  
  9. # This is all done in table format, creating an index (or album) of the graphics 
  10. # in the directory.
  11. #
  12. # The directory specified must be a directory under the DocumentRoot defined
  13. # in the Web Server's SRM.CFG file.
  14. #
  15. # The directory to album is specified on the URL line following the name of 
  16. # the script.
  17. #
  18. # Example: http://server:port/perl/album.pl/images gives an album of the 
  19. # "images" directory.
  20. # Descriptions of the graphic files are given by creating a text file whose 
  21. # name is the same as the graphic file, except with an extension of .TXT.  
  22. # The description file must be in the same directory as the graphic file.  
  23. # The description will be displayed on the album page.
  24. #
  25. # This may not be the most efficient script in the world <g>, but it works.
  26.  
  27. # Standard opening commands for a Web Server Perl CGI script
  28. require("cgi-lib.pl");
  29. print &PrintHeader;
  30. &ReadParse;
  31.  
  32. # Set up the necessary variables
  33. $dir = $ENV{"PATH_TRANSLATED"};     #The directory to look in for images
  34.                     #This is the real dir on the server
  35. if ( rindex($dir, "/") != length($dir)-1 ) {
  36.   $dir = $dir."/";             #If $dir doesn't end in '/', add it
  37. }
  38. $filespec = $dir."*.???";            #filespec will be globbed for image files
  39. $path = $ENV{"PATH_INFO"};        #The dir to the images relative to the DocumentRoot
  40. if ( rindex($path, "/") != length($path)-1 ) {
  41.   $path = $path."/";            #If $path doesn't end in '/', add it
  42. }
  43. @types = ("GIF", "JPG");        #The valid extensions to search for
  44.  
  45. #--------- HTML Document Setup --------------
  46. print "<html>\n";
  47. print "<title>Album of $path</title>\n";
  48. print "<body bgcolor=#ffffff>\n";
  49. print "<h3>Album of $path</h3>\n";
  50. print "Click on the file name of an image to have the full file delivered.<hr>\n";
  51.  
  52. #-------- Build Table -------------------
  53. # Set up the table for this .GIF/.JPG
  54. print "<table border=1>\n";
  55. opendir(DIR, $filespec);    #Has to be opened with globbing because of a Perl bug.
  56. foreach $file (sort(readdir(DIR))) {    #Loops through pictures, making the index as it goes.
  57.   $name = substr($file, 0, length($file)-4);
  58.   $ext = substr($file, length($file)-3, 3);
  59.   if (grep(/$ext/, @types) > 0) {
  60.     print "<tr>\n";
  61.     print "  <td valign=top align=left>\n";
  62.     # Display link
  63.     print "    [<a href=$path$file>";
  64.     print "      $file";
  65.     print "    </a>]\n";
  66.     # Display text description
  67.     if (-e "$dir$name.TXT") {
  68.       open(DESC, "$dir$name.TXT");
  69.       while (!eof(DESC)) {
  70.         print <DESC>;
  71.       }
  72.     }
  73.     else {
  74.       print "No description available.\n";
  75.     }
  76.     print "  </td>\n";
  77.     # Display picture
  78.     print "  <td>\n";
  79.     print "    <img src=$path$file border=0>\n";
  80.     print "  </td>\n";
  81.     print "  </td>\n";
  82.     print "</tr>\n";
  83.   }
  84. }
  85. # When all the files are listed, close the directory
  86. closedir(DIR);
  87. # Close the table
  88. print "</table><p>\n";
  89.  
  90. #---------- HTML Document Wrap-up ------------
  91. print "</body>\n";
  92. print "</html>\n";
  93.