0

我对 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() 函数,它可以按字母顺序对它们进行排序,但是我留下了一个数组。然后我使用 implode 函数将数组连接在一起,但是在那之后我被困在做什么。

任何帮助将不胜感激!

干杯。

4

2 回答 2

2

您可以使用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>';
}
于 2012-02-14T06:48:19.797 回答
1

数组有什么问题?如果您使用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>';

    }
}
于 2012-02-14T07:12:20.187 回答