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

发布于 2025-01-05 10:25:12 字数 1593 浏览 1 评论 0原文

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

经过无数次 Google 搜索之后,我找到了一个 PHP 脚本,它可以完成我想要它做的事情,在查看代码并稍微操作它之后,它可以完美地与我的网站配合使用;只不过这些图像不是按字母顺序排列的。

这是我使用 scandir() 函数的代码

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

    }
}

,该函数可以按字母顺序对它们进行排序,但是我留下了一个数组。然后我使用 implode 函数将数组连接在一起,但之后我就陷入了困境。

任何帮助将不胜感激!

干杯。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城旧梦 2025-01-12 10:25:12

您可以使用 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>';
}

You can use glob() to get the files from a directory, sorted alphabetically:

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

To iterate over your files, use a foreach loop:

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>';
}
情深缘浅 2025-01-12 10:25:12

数组出了什么问题?
另外,如果您使用 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>';

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文