2011-03-29 80 views
1

在我的php項目中,我有一個名爲Gallery1的文件夾(路徑是images/gallery1)。其中包含名爲1.jpg,2.jpg,20.jpg,8.jpg等的圖像。我想按照升序顯示該文件夾中的所有圖像(1.jpg,2.jpg,8. jpg,20.jpg等等)。有人知道嗎?以特定順序顯示文件夾中的圖像

在此先感謝

+0

例如,這可以幫助你[排序](http://stackoverflow.com/questions/260387/ordering-a-list-of-files-in-一個文件夾,使用的PHP) – Dotnet 2011-03-29 11:00:37

回答

5
<?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 
?> 
3

你可以使用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 
) 
相關問題