PHP排序opendir文件包括


PHP sorting opendir file includes

我正在尝试对opendir()列表进行排序,以便它可以按照我命名的顺序显示信息。

目录中的每个文件称为1_something.php、2_something.php、3_something.php等。 这些文件是模板化 HTML 的一小段,我将其更改为我需要它说的任何内容。

我正在使用下面的代码来拉取和显示这些文件:

$dir = "./portfolio"; 
if($handle = opendir($dir)) { 
    while($file = readdir($handle)) { 
        clearstatcache(); 
        if(is_file($dir.'/'.$file)) {
            include("portfolio/".$file);
        }
    } 
closedir($handle); 
} 

我一直在尝试创建名称数组并对它们进行排序,尽管我想我不知道数组的确切位置,排序发生的位置以及信息的实际显示发生的位置。

提前谢谢你。

嗯,有什么阻止您使用scandir吗?

http://sg.php.net/manual/en/function.scandir.php

$files = scandir($dir); // returns array of files, sorted alphabetically
foreach($files as $file) {
   // your code
}

劳归于@SiGanteng

我只需要添加另一行以确保它只查看真实文件:下面的代码

$dir = "./portfolio"; 
$files = scandir($dir); // returns array of files, sorted alphabetically
foreach($files as $file) {
    if(is_file($dir.'/'.$file)) {
        include("portfolio/".$file);
    }
}

这是按升序返回结果的最简单方法。
再次感谢思甘腾