【问题标题】:display images from a folder in a specific order以特定顺序显示文件夹中的图像
【发布时间】:2013-06-10 22:54:04
【问题描述】:

在我的 php 项目中,我有一个名为 Gallery1 的文件夹(路径是 images/gallery1)。其中包含名为 1.jpg、2.jpg、20.jpg、8.jpg 等的图像。我想按升序显示该文件夹中的所有图像(1.jpg、2.jpg、8.jpg、20.jpg ETC)。有人知道吗?

提前致谢

【问题讨论】:

标签: php


【解决方案1】:
<?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
?>

【讨论】:

    【解决方案2】:

    您可以使用按自然顺序排序的natsort

    来自 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
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-23
      • 2019-11-18
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2021-03-21
      • 1970-01-01
      相关资源
      最近更新 更多