Results 1 to 9 of 9

Thread: Checking number of files in a folder

  1. #1
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default Checking number of files in a folder

    I want to load images dynamically from a folder. Since I don't know how many images are in the folder I was wondering if there was a way to check that folder for the number of files so that I could then write a for loop to display them.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    You can grab all the jpg images in an array by calling:
    PHP Code:
    $images glob('./dir/*.jpg'); 
    Then if you still needed to count them:
    PHP Code:
    $imgcount count($images); 

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    List all of the images in a folder:
    PHP Code:
    <?php
    $imgdir 
    'images'// the directory, where your images are stored
    $allowed_types = array('png','jpg','jpeg','gif','bmp'); // list of filetypes you want to show
    $dimg opendir($imgdir);
    while(
    $imgfile readdir($dimg))
    {
    if(
    in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
      
    $a_img[] = $imgfile;
      
    sort($a_img);
      
    reset ($a_img);

    }
    $totimg count($a_img); // total image number
    for($x=0$x $totimg$x++)
    {
    $size getimagesize($imgdir.'/'.$a_img[$x]);
    // do whatever
    $halfwidth ceil($size[0]/2);
    $halfheight ceil($size[1]/2);
    echo 
    'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />';
    }
    ?>
    I also use a script from this site for a gallery http://www.dynamicdrive.com/dynamici...photoalbum.htm
    Last edited by james438; 09-17-2008 at 12:48 AM.

  4. #4
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    so then if I want to display them I would do it like this?

    Code:
    <?php
    $images = glob('./dir/*.jpg');
    
    for($i = 0; $i < $images-1; $i++)
    {
    echo "<img src='" . $images[$i] . "' />";
    ?>
    would this work...sorry I cannot test it at this time so I'm just trying to figure if that would work when I get home later...

  5. #5
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    @james

    That seems like a lot of fluff to do such an easy task. (you also have some errors in it)

    @mosh
    PHP Code:
    <?php
    $images 
    glob('./dir/*.jpg');
    foreach(
    $images as $img)
      echo 
    "<img src='" $img "' />";
    ?>

  6. The Following User Says Thank You to techietim For This Useful Post:

    Moshambi (09-16-2008)

  7. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    true, I have not completely debugged it yet, but why would you want to only display jpeg files?

    here is a more watered down version if you prefer, with the bugs removed . I wasn't able to figure out how to sort it in a case insensitive way yet though.
    PHP Code:
    <?php 
    $imgdir 
    'images';
    $allowed_types = array('png','jpg','peg','gif','bmp');
    $dimg opendir($imgdir); 
    while(
    $imgfile readdir($dimg)) 
    {
    if(
    in_array(strtolower(substr($imgfile,-3)),$allowed_types)) 

      
    $a_img[] = $imgfile
    }  
    }
    sort($a_img); 
    $totimg count($a_img);
    for(
    $x=0$x $totimg;) 
    {
    echo 
    "$a_img[$x]<br>";
    $x++; 

    ?>
    Last edited by james438; 09-15-2008 at 10:28 PM.

  8. #7
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Thank you so much techietim that worked wonders!

  9. #8
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Two hearts beating

    [Spam removed.]
    Last edited by djr33; 09-17-2008 at 06:47 PM.

  10. #9
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Quote Originally Posted by rjrenjian View Post
    Nurse: How do you feel after your operation? Patient: Quite alright, only I can feel two hearts beating inside me. Nurse: No wonder the doctor who operated on you was looking for his replica rolex everywhere just now.
    Okay, I must say that was the funnest spam post I've ever seen.

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
  •