iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 11, 2008
Improving accessibility for motor impaired users
WebDevTips UK
 
May 11, 2008
10 ways to orientate users on your site
WebDevTips UK
 
May 11, 2008
Web Design Clinic - Rros restoration camp 2006
About
 
May 10, 2008
The Moods of Facebook
About
 
May 9, 2008
CSS 1 properties are a great start
About
 
May 9, 2008
Reader Question: How do you get fancy fonts?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /File Manipulation

Simplest PHP Image Gallery 

  Views:    3573
  Votes:    6
by Chief Programmabilities 1/18/05 Rating: 

Synopsis:

Create a "Simplest PHP Image Gallery" from the images in a directory according to whatever extension(s) you want.
Pages: 
The Article

Suppose you want to create a gallery that displays in a Web page all the images in a specified directory. You can use the opendir and readdir functions to do this. Below is a script that creates an image gallery.

<html><head><title>Image Gallery</title></head><body>    
<?php
      $dir = "../images/";                                              #8
      $dh = opendir( $dir );                                            #9
      while( $filename = readdir( $dh ) ) {                             #10
           $filepath = $dir.$filename;                                  #12
           if( is_file( $filepath ) and ereg( "\.jpg$", $filename ) ) { #13
                $gallery[] = $filepath;
           }
      }
      sort( $gallery );                                                 #16
      foreach( $gallery as $image ) {                                   #17
           echo "<hr/>";
           echo "<img src='$image'><br/>";
      }
?>
</body></html>


Notice the line numbers at the end of some of the lines in the script. The following discussion
of the script and how it works refers to the line numbers in the script listing:
Line 8: This line stores the name of the directory in $dir for use later in the program. 
Notice that the / is included at the end of the directory name. Don't use \, even in Windows.
Line 9: This line opens the directory.
Line 10: This line starts a while loop that reads in each file name in the directory.
Line 12: This line creates the variable $filepath, which is the complete path to the file.
Line 13: This line checks to see whether the file is a graphics file by looking for the .jpg 
extension. If the file has a .jpg extension, the complete file path is added to an array
called $gallery.
Line 16: This line sorts the array using the sort function so the images are displayed in 
alphabetical order.
Line 17: This line starts the foreach loop that displays the images in the Web page.
A working example of this PHP image gallery script can be found here: 
http://programmabilities.com/images/.


Chief Programmabilities
Programmabilities.com

Pages: 

Similar/related articles:


 
  Sponsors