简体   繁体   English

使用readdir()时排序输出

[英]Ordering output when using readdir()

I'm fairly new to PHP and have been using PHP's readdir() to look into a folder full of images and render them out dynamically based on how many images there are in that folder. 我对PHP相当陌生,一直在使用PHP的readdir()来查看一个装满图像的文件夹,并根据该文件夹中的图像数量动态地将它们渲染出来。 Everything works great, but one thing I've noticed is that the images are not displayed in the order that they appear on my local machine HD. 一切正常,但我注意到的一件事是,图像未按照它们在本地计算机HD上出现的顺序显示。

So my question to anyone who knows PHP is, is there way of using PHP to read the contents of a folder AND display them in order without having to rename the actual file names eg 01.jpg, 02.jpg etc etc? 所以我对任何了解PHP的人的问题是,有没有办法使用PHP读取文件夹的内容并按顺序显示它们,而不必重命名实际的文件名,例如01.jpg,02.jpg等?

Have a look at the glob() function, it returns files alphabetically sorted by default: 看一下glob()函数,它返回默认情况下按字母顺序排序的文件:

$files = glob('/some/path/*.*');

Bonus, you can filter just images, and leave out directories. 另外,您可以只过滤图像,而忽略目录。

readdir likely just takes the file system order. readdir可能只是接受文件系统命令。 Which is alphabetical on NTFS, but seemingly random on most Unix filesystems. 在NTFS上按字母顺序排列,但在大多数Unix文件系统上看似随机。 The documentation even says as much: »The entries are returned in the order in which they are stored by the filesystem.« 文档甚至说了这么多:»条目按文件系统存储的顺序返回。«

So you'd have to store the list in an array and sort that based on how you would like them to be sorted. 因此,您必须将列表存储在数组中,然后根据您希望对它们进行排序的方式对其进行排序。

The php manual says: php手册说:

string readdir ([ resource $dir_handle ] )
Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.

Meaning they should appear the same way. 意味着它们应该以相同的方式出现。

More information found in the manual . 有关更多信息,请参见手册

Why not apply one of the sort-functions of PHP ? 为什么不应用PHP排序功能之一

$files = readdir( $theFoldersPath );
sort( $files  );

Here is what I came up with in answer (together with the help of the people who posted) to my own question. 这是我对自己的问题的答案(在发帖人的帮助下)。

<?php 

$dir = "low res";
$returnstr = "";

// The first part puts all the images into an array, which I can then sort using natsort()
$images = array();

if ($handle = opendir($dir)) {
    while ( false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".."){
            $images[] = $entry;
        }
    }
    closedir($handle);
}

natsort($images);
print_r($images);

$newArray = array_values($images);

// This bit then outputs all the images in the folder along with it's own name

foreach ($newArray as $key => $value) {
    // echo "$key - <strong>$value</strong> <br />"; 

    $returnstr .= '<div class="imgWrapper">';
    $returnstr .= '<div class="imgFrame"><img src="'. $dir . '/' . $value . '"/></div>';
    $returnstr .= '<div class="imgName">' . $value . '</div>';
    $returnstr .= '</div>';


}

echo $returnstr;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM