SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Seattle
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    display contents of an image folder dynamically

    I am pretty new to PHP and am creating a site to display photos in a gallery. I have the site placeholders made but I have a folder with 120 jpgs now that I need to write in each file name. I was thinking that it would be much faster to somehow get the contents of the folder and loop through it to dynamically show all the pictures in that foder instead of writing the file names in one by one. Anyone tried this type of stunt before? Thanks for any advice or feedback.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This little script will walk through the content of the folder and put all the images in an array that you can use later.

    PHP Code:
    <?php

    // the name of the folder where the images are stored ( no trailing slash! ).
    $imgDir 'images';

    // open it
    $handle opendir($imgDir);

    // the array that will store the name of the images
    $images = array();

    // get the content of the folder
    while(( $file readdir($handle)) !== FALSE )
    {
      if( 
    is_file"$imgDir/$file) )
      {
        
    $images[] = $file;
      }
    }

    // now you can access all the names of the images in the array $images

    ?>

  3. #3
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Seattle
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using the array..

    So then if I put the $images[1], $images[2] etc. I can get image value in the array to show up? Sorry if I'm a little slow...

    The situation is that I have a gallery of photos that display 12 images on each page. My photographer sent me a few CDs with around 130 images each. I found myself renaming each file to match up with the path in my HTML file which is taking way too long. Then I have to update the PayPal form to whichever number the picture is so he will know what picture a person is really ordering. I'm thinking of doing a combination of useing a naming convention for his photos as well as an array for each page that I can start using dynamically. So to create a new page of photos all I will really need to do is point the PHP code to the new folder with the 12 images and they should show up. Am I on the right track here? Am I missing something obvious? Thanks a ton for any feedback!

  4. #4
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Seattle
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still confused, might be going about this all wrong

    Here's where I'm running into an error message. I can get the image to show up if I just do echo("<img src="images/$images[2]">") but when I try to put in my HTML it breaks. Do I need to embed the HTML in the PHP code or is there an easy way to just call the array value where I need it like <img src="images/<?php $images[2] ?>">. Thanks to anyone that can lead me in the right direction. Here's what I have so far:
    <TABLE<tr><td>

    <?php
    // the name of the folder where the images are stored ( no trailing slash! ).
    $imgDir = 'images';

    // open it
    $handle = opendir($imgDir);

    // the array that will store the name of the images
    $images = array();

    // get the content of the folder
    while(( $file = readdir($handle)) !== FALSE )
    {
    if( is_file( "$imgDir/$file" ) )
    {
    $images[] = $file;
    }
    }

    echo("<a href="javascriptopUp('images/$images[2]' , 'PHOTO: $images[2]' , '')"><img src="images/$images[2]" width="130" height="87" border="0"></a><br>")
    ?>

    </td></tr></table>

  5. #5
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Seattle
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    harder question

    I can call the array now successfully and I can populate the areas I need to. Step one completed! Now I am faced with putting the $images[] array values in one by one but at least I can just change the mapping and use the code for any folder. I have about 120 images in a folder. I am thinking that the next step would be to create a loop and generate the whole page on the fly so I don't have to have a seperate PHP file for every 12 images. I would need some way to have a "go to next N" or "go back to previous N" links also. Here's what I have so far if anyone is watching:
    <?php
    // the name of the folder where the images are stored ( no trailing slash! ).
    $imgDir = 'gallery';
    $imgThumb = 'gallery/Thumbs';
    // open it
    $handle = opendir($imgDir);
    // the array that will store the name of the images
    $images = array();
    // get the content of the folder
    while(( $file = readdir($handle)) !== FALSE )
    {
    if( is_file( "$imgDir/$file" ) )
    {
    $images[] = $file;
    }
    }
    // open it
    $handle = opendir($imgThumb);
    // the array that will store the name of the images
    $imagesThumb = array();
    // get the content of the folder
    while(( $file = readdir($handle)) !== FALSE )
    {
    if( is_file( "$imgThumb/$file" ) )
    {
    $imagesThumb[] = $file;
    }
    }
    ?>
    <TABLE cellpadding="1" cellspacing="0" border="0"><tr><td>
    <a href="javascriptopUp('<?=$imgDir?>/<?=$images[1]?>' , '<?=$images[1]>' ,'')"><img src="<?=$imgThumb?>/<?=$images[1]?>"></a><br>
    </td></tr></table>
    I have 4 of these per row and 3 rows total. I just switch the number of the array and it works. Now I just need to figure out how to dump all my files into the array and have the page know which ones to display.

    Anyone ever done this or know of a good example I could learn from? Thanks again for any help for this PHP newbie. peaceout.

  6. #6
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Seattle
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well after some reading I figured it out. This code will parse through my two folders, one for the thumbnails and one holding the full size images, both with the same file names. Then I set up the start and end variables and use them inside of a link to allow navigation of the entire folder showing 12 images at a time. I just use array[] syntax to populate the mappings to my images, links and paypal shopping cart. To create a new gallery all I need to do is copy the code and replace the $imgDir and $imgThumb directories and that's it. Here's the code if anyone is watching:

    // the name of the folder where the images are stored ( no trailing slash! ).
    $imgDir = 'gallery/3-8-04FGallery';
    $imgThumb = 'gallery/3-8-04SGallery';
    // open it
    $handle = opendir($imgDir);
    // the array that will store the name of the images
    $images = array();
    // get the content of the folder
    while(( $file = readdir($handle)) !== FALSE )
    {
    if( is_file( "$imgDir/$file" ) )
    {
    $images[] = $file;
    }
    }
    $result = count ($images);
    $start=$HTTP_GET_VARS['start'];
    // Populate the demo array
    $i=0;while ($i<=$result){$countvar[]=$i;$i++;}
    // the number of items displayed on one screen.
    $increase=12;
    // if there is no start value set, set start to 0
    if(!$start){$start=0;}
    // end of display is start plus increase
    $end=$start+$increase;


    $images=array_slice($images,$start,$increase);

    // If start is equal or bigger than increase, display the previous
    // link, which sets start to start-increase
    if ($start >= $increase){
    echo "<a href=\"".$HTTP_SERVER_VARS['PHP_SELF']."?start=".($start-$increase)."\">PREVIOUS</a> ";
    }



    // If the end is smaller than the amount of items in increase, display the next
    // link, which sets start to start+increase
    if ($end < sizeof($countvar)){
    echo " <a href=\"".$HTTP_SERVER_VARS['PHP_SELF']."?start=".($start+$increase)."\">NEXT</a>";
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •