Simple PHP image gallery – a tutorial

6 Comments    October 3, 2005 09:18

In my first programming tutorial I will show you how to create a simple image gallery with PHP. For this program to work you need to upload your images in two separate folders on your server. One folder containing the thumbnails and one folder with the full size images. The names for the images should be the same for the full size image and the corresponding thumbnail.

This is what the code looks like.

$thumbs_dir = "./thumbs/";
 $fullsize_dir = "./full/";
 $dir = opendir($thumbs_dir);
 while ($file = readdir($dir)) {
      if ($file != "." && $file != ".."; ) {
           echo "";
     }
}

Lets’t take a look at the code. First on line 1 and 2 we decalare variables for the thumb and fullsize folders. Then on line 3 we open the thumbs folder for reading then on line 4-9 we loop through all the images in the thumbs directory and outputs the thumbnails with a link to the full size image. Now that wasen’t that hard was it?

All you have to do is change the folder names for $thumbs_dir and $fullsize_dir to suite your installation. The output looks something like this. (Example from my moblog thumbnail folder).

Screenshot


  6 Comments   Comment

  1. 13 years ago  

    Hi thanx alot for that`s start..
    but is that all the tutorials?
    Hamza

  2. 13 years ago  

    Isn’t there a bug in the code above?

    I’m not quite sure, but i think, this line:
    4. while ($file = readdir($thumbs_dir)) {

    has to look like this:
    4. while ($file = readdir($tdir)) {

    And don’t forget to change the ‚Äú‚Äú’s into the correct ones, the code won’t work with them.

    n”);
    echo (“nn”);
    }
    }
    closedir($dir);
    ?>

    Nice site though, keep the good work up 😉
    (& turn a blind eye on my bad spelling)

  3. 13 years ago  

    Sry can’t post the code %-)
    Second try:

    >?php
    $thumbs_dir = “./images/thumbs/“;
    $fullsize_dir = “./images/“;
    $dir = opendir($thumbs_dir);
    while ($file = readdir($dir)) {
    if ($file != “.“ && $file != “..“) {
    echo (“n“);
    echo (“
    nn“);
    }
    }
    closedir($dir);
    ?>

  4. 12 years ago  

    “;
    echo ““;
    }
    }
    closedir($dir);

    ?>

    Hey guys. I came across this site and was playing around with the script.
    By the way “aightgenosse” you forgot the echo portion of the doucment it should be

    echo (“n”);
    echo (“rn”);

    //and to safley add quotes use “target” as displayed.

    echo ““;
    echo “
    “;

    I do have a problem however, when I try to view my gallery it dose not display any pictures what so ever instead it has really small boxes, allmost like lines. now only when I click it and the new window is opened. Btw the directories are right. I was wondering if you could help with this problem.

    Thank you.
    P.S. good tut btw.

  5. G

    12 years ago  

    >?php
    $thumbs_dir = “./images/thumbs/”;
    $fullsize_dir = “./images/”;
    $dir = opendir($thumbs_dir);
    while ($file = readdir($dir)) {
    if ($file != “.” && $file != “..”) {
    echo (“n”);
    echo (“rn”);
    }
    }
    closedir($dir);
    ?>

Leave a Comment

You must be logged in to post a comment.