开发者

display images from a folder in a specific order

开发者 https://www.devze.com 2023-02-20 07:18 出处:网络
In my php project I have a folder named Galle开发者_StackOverflow中文版ry1 (path is images/gallery1). Which contains images named 1.jpg, 2.jpg, 20.jpg, 8.jpg etc. I want to display all the images from

In my php project I have a folder named Galle开发者_StackOverflow中文版ry1 (path is images/gallery1). Which contains images named 1.jpg, 2.jpg, 20.jpg, 8.jpg etc. I want to display all the images from that folder in ascending order (1.jpg, 2.jpg, 8. jpg, 20.jpg etc). Does anyone know this?

Thanks in advance


<?php
// Find all files in that folder
$files = glob('images/gallery1/*');

// Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
natcasesort($files);


// Display images
foreach($files as $file) {
   echo '<img src="' . $file . '" />';
}

// ???
// Profit :D
?>


You could use natsort which sorts by natural order.

Example from PHP.net

$array1 = array("img12.png", "img10.png", "img2.png", "img1.png");
natsort($array1);

Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号