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


Sort alphabetically with opendir()

我对PHP还很陌生,所以我仍在学习最基本的知识,但我正在尝试创建一个图像库。

经过无数次谷歌搜索,我发现了一个PHP脚本,它可以做我想让它做的事情,在查看了代码并对其进行了轻微的操作后,它与我的网站完美配合;只是图像不是按字母顺序排列的。

这是代码

$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>';
    }
}

我使用了scandir()函数,它可以按字母顺序对它们进行排序,但我只剩下一个数组。然后,我使用内爆函数将数组连接在一起,但在那之后,我被该做什么所困扰

如有任何帮助,我们将不胜感激!

干杯。

您可以使用glob()从目录中获取文件,按字母顺序排序:

$files = glob('gifs/animals/*.{gif,jpg,png}', GLOB_BRACE);

要迭代文件,请使用foreach循环:

foreach($files as $file){
    $title = str_replace("_"," ",$file);
    echo '<li><a href="'.$name.'">';
    echo '<img src="thumbs/'.basename($file).'" class="pictures" alt="'.basename($file).'" />';
    echo '</a>';
    echo ''.$title.'';
    echo '</li>';
}

数组出了什么问题?此外,如果您使用pathinfo来获取文件名和扩展名,效果会更好。

$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>';
    }
}