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
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?
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
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
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
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?
Might be that you have subfolders, perhaps? As the code I posted isn't recursive. Try loading a specific folder with images in it.