简体   繁体   中英

gallery to directory Php showing only the last 4 images

I need to create a full page gallery showing only the last 4 images from a directory, i don't have idea how to introduce only the last 4 images saved in the directory, any idea?

<?php
$directory= "img";
$dirint = dir($directory);

while (($archivo = $dirint->read()) !== false){

    if (!preg_match('/gif/i', $archivo)
       || !preg_match('/jpg/i', $archivo)
       || !preg_match('/png/i', $archivo)) {

        for($x = 0; $x < 3; $x++) {
            echo '<img src="'.$directory."/".$archivo.'">'."\n";
            echo "<br>";
            $x++;
         }
    }
}

$dirint->close();

?>

Hi i resolve my problem with a slice to the array dirint, thanks for the help here my solution:

<?php 
$img_dir = "THEPATHTOYOURDIRECTORY";
$images = scandir($img_dir);
$imageslast = array_slice($images,-4,6);
rsort($imageslast);

with imagelast i make a slice to the array counting the "." and ".." i sorted in reverse to take the last image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM