2012-03-09 32 views
1

我修改並清除了其他人寫的PHP腳本。在我的WAMP服務器上,它按字母順序列出了圖像(它們全部命名爲001.jpg〜110.jpg),但是在活LAMP服務器上,我認爲它們是按修改日期組織的......不管它是不是文件名。它們都是JPEG圖像,所以我不擔心按類型排列。PHP:圖像腳本不按字母順序列出圖像

那麼,我該如何修改這個腳本來按字母順序列出圖片呢?

function getPictures() 
{ 
global $page, $per_page, $has_previous, $has_next; 

if ($handle = opendir('tour/')) 
{ 
    $lightbox = rand(); 
    echo '<ul id="pictures">'; 

    $count = 0; 
    $skip = $page * $per_page; 

    if ($skip != 0) {$has_previous = true;} 

    while ($count < $skip && ($file = readdir($handle)) !== false) 
    { 
    if (!is_dir($file) && ($type = getPictureType($file)) != '') {$count++;} 
    } 

    $count = 0; 

    while ($count < $per_page && ($file = readdir($handle)) !== false) 
    { 
    if (!is_dir($file) && ($type = getPictureType($file)) != '') 
    { 
    if (!is_dir('thumbs/')) {mkdir('thumbs/');} 
    if (!file_exists('thumbs/'.$file)) {makeThumb('tour/'.$file,$type);} 

    echo '<li><a href="tour/'.$file.'" rel="lightbox['.$lightbox.']">'; 
    echo '<img src="thumbs/'.$file.'" alt="" />'; 
    echo '</a></li>'; 
    $count++; 
    } 
    } 
    echo '</ul>'; 

    while (($file = readdir($handle)) !== false) 
    { 
    if (!is_dir($file) && ($type = getPictureType($file)) != '') 
    { 
    $has_next = true; 
    break; 
    } 
    } 
} 
} 
+1

把文件名中的數組,那種和然後輸出列表。 – Niko 2012-03-09 20:22:21

+0

[在php中按字母順序排序和顯示目錄列表使用opendir()](http://stackoverflow.com/questions/884974/sort-and-display-directory-list-alphabetically-using-opendir-in-php ) – j08691 2012-03-09 20:24:28

+0

我在想數組,這不是這個問題的一個騙局。 – John 2012-03-09 20:29:01

回答

0

看起來像一個「收藏夾」功能,如果是的話在這裏就是我上面貼的功能修改後的版本...

function getPictures() 
{ 
if ($handle = opendir('tour/')) 
{ 
    global $page, $per_page, $has_previous, $has_next; 
    $lightbox = rand(); 
    echo '<ul id="pictures">'; 
    $count = 0; 
    $skip = $page * $per_page; 

    $file = scandir('tour/'); 
    $images = array(); 

    foreach ($file as $key => $value) 
    { 
    if (!is_dir('tour/'.$value) && ($type = getPictureType('tour/'.$value)) != '') 
    { 
    array_push($images,$value); 
    } 
    } 

    natsort($images); 

    $count = 0; 
    $start = $per_page*$page; 
    $end = $start+$per_page - 1; 

    foreach ($images as $key => $value) 
    { 
    if ($key>=$start && $key<=$end) 
    { 
    echo '<li><a href="tour/'.$value.'" rel="lightbox['.$lightbox.']"><img src="thumbs/'.$value.'" alt="" /></a></li>'; 
    $count++; 
    } 
    } 
    $not_first = $end+1; 
    if ($key>$end) {$has_next = true;} 
    if ($not_first!=$per_page) {$has_previous = true;} 

    echo '</ul>'; 
} 
} 
2

而不是使用readdir,你可以使用scandir,它默認的字母順序排列。

默認情況下,排序順序按字母順序升序排列。如果 可選sorting_order設置爲SCANDIR_SORT_DESCENDING,那麼 排序順序按字母順序降序排列。如果它設置爲 SCANDIR_SORT_NONE,那麼結果是未排序的。

請記住,scandir返回一個文件名數組,而readdir返回一個單一的項名稱。

或者,您可以將文件名讀入數組中,然後使用natsort對其進行排序。

// Orders alphanumeric strings in the way a human being would 
natsort($arr); 

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