doupang9080 2012-02-14 06:35
浏览 127
已采纳

使用opendir()按字母顺序排序

I'm quite new to PHP so I'm still learning the very basics, however I'm trying to create an image gallery.

After countless Google searches later, I found a PHP script that does what I want it to do, and after looking at the code and manipulating it slightly it was working perfectly with my site; except that the images were not in alphabetical order.

This is the code

$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash


function getPictureType($ext) {
    if ( preg_match('/jpg|jpeg/i', $ext) ) {
        return 'jpg';
    } else if ( preg_match('/png/i', $ext) ) {
        return 'png';
    } else if ( preg_match('/gif/i', $ext) ) {
        return 'gif';
    } else {
        return '';
    }
}

function getPictures() {
    global $max_width, $max_height, $imagedir;
    if ( $handle = opendir($imagedir) ) {
        $lightbox = rand();
        echo '<ul id="pictures">';
        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) ) {
                $split = explode($imagedir, $file); 
                $ext = $split[count($split) - 1];
                if ( ($type = getPictureType($ext)) == '' ) {
                    continue;
                }

                $name = substr($file, 0, -4);
                $title = str_replace("_"," ",$name);
                echo '<li><a href="'.$name.'">';
                echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
                echo '</a>';
                echo ''.$title.'';
                echo '</li>';
            }
        }
        echo '</ul>';

    }
}

I've used the scandir() function which works in sorting them alphabetically, however I was left with an array. I then used the implode function to join the array together, however after that I was stuck with what to do.

Any help would be greatly appreciated!

Cheers.

  • 写回答

2条回答 默认 最新

  • duan1979768678 2012-02-14 07:12
    关注

    What's wrong with the arrays? Also it would be better if you use pathinfo to obtain the filename and the extension.

    $max_width = 100;
    $max_height = 100;
    $imagedir = 'gifs/animals/'; //Remember trailing slash
    
    
    function getPictureType($ext) {
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }
    
    function getPictures() {
        global $max_width, $max_height, $imagedir;
        if ( $files = scandir($imagedir) ) {
            $lightbox = rand();
            echo '<ul id="pictures">';
            foreach ($files as $file) {
                $full_path = $imagedir.'/'.$file;
                if ( !is_dir($file) ) {
                    $finfo = pathinfo($full_path); 
                    $ext = $finfo['extension'];
                    if ( ($type = getPictureType($ext)) == '' ) {
                        continue;
                    }
    
                    $name = $finfo['filename'];
                    $title = str_replace("_"," ",$name);
                    echo '<li><a href="'.$name.'">';
                    echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
                    echo '</a>';
                    echo ''.$title.'';
                    echo '</li>';
                }
            }
            echo '</ul>';
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)