1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Show Images from a directory in PHP and limiting amount of images to display

Discussion in 'PHP' started by mrbrown123, Apr 6, 2015.

  1. #1
    Hi There,
    I am using PHP to show uploads of images in a directory. I just want to show latest images of around 8- 10

    the code im using shows all the images. Here the code
    ?php
    
    echo scanDirectoryImages("images");
    
    /**
    * Recursively search through directory for images and display them
    *
    * @param  array  $exts
    * @param  string $directory
    * @return string
    */
    function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
    {
        if (substr($directory, -1) == '/') {
            $directory = substr($directory, 0, -1);
        }
        $html = '';
        if (
            is_readable($directory)
            && (file_exists($directory) || is_dir($directory))
        ) {
            $directoryList = opendir($directory);
            while($file = readdir($directoryList)) {
                if ($file != '.' && $file != '..') {
                    $path = $directory . '/' . $file;
                    if (is_readable($path)) {
                        if (is_dir($path)) {
                            return scanDirectoryImages($path, $exts);
                        }
                        if (
                            is_file($path)
                            && in_array(end(explode('.', end(explode('/', $path)))), $exts)
                        ) {
                            $html .= '<a href="' . $path . '"><img src="' . $path
                                . '" style="max-height:50px;max-width:50px" /></a>';
                              
                        }
                    }
                }
            }
            closedir($directoryList);
        }
        return $html;
    }
    
    Code (markup):
    Anyone have a quick fix to add to this to limit the photos to latest 10?

    many thanks
     
    mrbrown123, Apr 6, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well. Do you have a naming convention for the images? Or would you have to get modified date to discern which ones are the latest?
     
    PoPSiCLe, Apr 6, 2015 IP
  3. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Hi, well the images are displaying random so I am not to concerned with the latest showing or the order. Just limiting them to say 10.
    otherwise they will just continue down the page content. And for the life of me cant find a fix for this. Thanks for your reply
     
    mrbrown123, Apr 6, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    I can get you a code example, but it'll have to wait till I'm back at my computer.
     
    PoPSiCLe, Apr 6, 2015 IP
  5. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #5
    Cheers TY
     
    mrbrown123, Apr 6, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Bah, got a little carried away, so I managed to reduce the functionality somewhat (the recursiveness got the axe) - I can redo it, but it's late, so I'll have to get back to it. Here's at least one effort:
    
    <?php
    
    print_r(scanDirectoryImages("files/images"));
    
    /**
    * Recursively search through directory for images and display them
    *
    * @param  array  $exts
    * @param  string $directory
    * @return string
    */
    function scanDirectoryImages($directory, $exts = array('jpeg', 'jpg', 'gif', 'png')) {
      if (substr($directory, -1) == '/') {
      $directory = substr($directory, 0, -1);
      }
      $html = '';
      $limit = 10;
      if (is_readable($directory) && (file_exists($directory))) {
      $files = array_slice(array_diff(scandir($directory), array('..','.')), 0, $limit);
      foreach ($files as $key => $file) {
      $ext = explode('.',$file);
      if (in_array($ext[1],$exts)) {
      $path = $directory. '/' . $file;   
      $html .= '<a href="' . $path . '"><img src="' . $path . '" /></a>';
      }
      }
      }
      return $html;
    }
    
    ?>
    
    Code (markup):
    You'll notice that I used scandir instead of opendir and readdir, and I used a simple array_slice to limit to 10 (this can of course be adjusted, as can the array_diff-filter). I removed the width and height from the images - you really shouldn't do that in HTML, as it only makes the showing of the images different, it doesn't really do anything with the image itself, which means that if you want to have the size be 50px by 50px, you should do some sort of thumbnail-generation on upload, or on read.

    Should at least give you some ideas :)
     
    PoPSiCLe, Apr 6, 2015 IP
  7. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #7
    Thankyou will test it tonight. I needed the image size as the directory images are all the same and used in another page function. The images are from users uploads of selfies from cam. I using this code to show a row of latest user selfies on the main social page using just iincludes PHP with the size as its cleaner for the page upload. Thank for the code. going to have a play with it. cheers ;)

    had a check, couldnt get it to work.. going to try and mash the codes up a little
     
    Last edited: Apr 7, 2015
    mrbrown123, Apr 7, 2015 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    You did change the folder name, I hope? I did test it on a private server, it should work without too much trouble... did you get any errors?
     
    PoPSiCLe, Apr 7, 2015 IP
  9. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #9
    Yes changed the folder name but had a blank. no errors. maybe i missed something
     
    mrbrown123, Apr 8, 2015 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Might be that you have subfolders, perhaps? As the code I posted isn't recursive. Try loading a specific folder with images in it.
     
    PoPSiCLe, Apr 8, 2015 IP