website promotion banner
eturnkeys
Your Ad Here
Web Programming  Home Web Programming PHP Automated PHP Photo Album
rss

Automated PHP Photo Album

Author: ric More by this author


Automated PHP Photo AlbumThis script makes managing large online photo albums and portfolios much simpler. Each album is stored in a separate folder. The script uses the folder names as the album names. To add a new album, all you need to do it make a new folder.

Upload some pictures

  1. First, we need to FTP the directory that the main website's in and create a new folder that will store the albums. I'm calling mine 'photos'.
  2. Next, we need to create folders inside the 'photos' folder to hold the albums. So, for example, I want one to hold my holiday photos from 2005 and my holiday photos from 2006. I'll create one folder called 'Holiday-Photos-2005' and 'Holiday-Photos-2006'. Notice the hyphens instead of spaces so that we don't get errors all over the place (we'll get rid of them when we display the names on the page). I've also capitalised the titles because that's how they'll appear on screen. For security reasons (see section 3), you shouldn't use % signs in the name. Ampersands, dollar signs and question marks will confuse the code as well.
  3. Now upload the photos to their appropriate folders.

Find the albums

Here comes the code. This is going into 'albums.php', which I'm storing in the same place as the rest of the website.

  1. Let's make sure we do things properly, so we're going to use the full path to the photos all the time. Let's make sure it's accurate and get PHP to do it for us, using:

    $path = getcwd();

  2. Now we have to scan the 'photos' folder to get the names of the folders. Remember to change '/photos' to your folder name. Each file within the 'photos' folder is now scanned and added to the $album array . While we're going through the array, we have to make sure that we only include folders using is_dir. We then make links to each album. An album is viewed at a page I'm calling 'album.php'. I've used str_replace to get rid of the hyphens in the album names.

    foreach (scandir("$path/photos") as $album) {

    if(is_dir($album)) {

    print "<a href=\"view.php?album=$album\">";

    print str_replace('-',' ',$album);

    print "</a>\n\n";

    }

    }

    In my case, the array would include 'Holiday-Photos-2005' and 'Holiday-Photos-2006'. The addresses of the links would look something like:

    www.example.com/view.php?album=Holiday-Photos-2005
    www.example.com/view.php?album=Holiday-Photos-2006

    If you're using Apache, you can use mod_rewrite in a .htaccess file to tidy this up.

Display the photos

Now we're working on the last page, 'view.php'.

  1. We want to make sure that the visitor has the $album variable when they get here. If not, we'll send them back to the albums page to select an album. We also want to make sure that the variable is safe and that someone isn't trying to hack us (you may want to add to the list of 'nasties'). The first thing on the page should be:

    $nasties = array("/","\","$");

    $album = str_replace($nasties,'',$_GET[ ' album ' ]);

     

    if(!$album) {

    header("Location: http://www.example.com/albums.php");

    }


  2. The final step is to retrieve the photos from their folder. We'll need to get the complete path again. Then we'll use the glob function to retrieve all JPG files from the folder. Don't forget to change the file extension if you need to or, if you want more than one file type, you could glob more than once. Like scandir , we'll get the results in an array that I've called $imgfile . It'll then go through each one and add the (X)HTML to display the picture. You might want to control the size of these, so that they appear as thumbnails and add a link on each one to a single larger picture, but I'm happy to have them all on the same page.

    $path = getcwd();

    foreach (glob("$path/photos/$album/*.jpg") as $imgfile) {

    print "<img src='$imgfile' />\n";

    }

Now just customise it and you're done. Time to share your photos with the world!



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Automated PHP Photo Album"