Automated PHP Photo Album

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>nn";

    }

    }

    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!


Related Articles

  • Photo-spangled designs
    Photos into design is like putting life into it. Without photos, the design would appear bland and boring to those who are seeing them. People, having short attention span, gets bored easily. It is when they see something interesting that their attention first is caught. Curiosity will follow...
  • Optimize Your Photos For The Web
    It doesnt matter if your emailing photos of your grandson or putting images of your latest product on your online store. Too many people dont consider optimizing their photos for the web. Weve all been on too many web pages where it seemed that the photos would never load, and sometimes they d...
  • Photography for Your Jewelry and Craft Website
    Not only is it important to take good photographs for your website, it is equally important to make sure that your pictures load quickly. In this article we will cover a number of important issues...
  • How to Chose Stock Photography for your Web Site
    So youve decided to take the plunge. You know that stock photography is an effective tool for your web business, but where do you start and how do you choose the stock photo thats right for you. Here are some tips to get you started so that you are happy with your choice. 1. Decide where you wan...
  • 17 Top Strategies to SEO
    1. Picking strategic keywords.
    2. Making sure these strategic keywords appear in the crucial locations on web pages.
    3. Including lots of relevant text content in the body of the HTML page.
  • Basic Web Design Principles
    Home page should clearly indicate what the site is about. Provide top level navigation on the first page, your logo, and tell to the visitor what he can found on your web site.
  • What IS A Blog?
    People maintained blogs long before the term was coined, but the trend gained momentum with the introduction of automated published systems, most notably Blogger at blogger.com...
  • Avoid Graphical Overload
    When designing a website, its easy to start loading it up with graphics. While tempting, you have to resist -- otherwise, youll end up with graphical overload.
  • Why a Web Site, and Why Web Hosting?
    Imagine having a filing system that could withstand a nuclear war. Hmm, I wouldnt like to prove it, but in theory that is what the Internet could resist with its virtual existence. If you transfer files of information around...
  • Using RSS to Explode Website Traffic
    Whats better: to get your readers to keep coming back because they remember you, or to have a little automated reminder there, telling them youre here, youve got new stuff, and they should come out and check it out? The answers obvious.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.