使用php中的opendir()按字母顺序排序和显示目录列表

时间:2009-05-19 20:55:29

标签: php sorting directory

php noob here - 我拼凑了这个脚本来显示一个带有opendir的文件夹中的图像列表,但我无法弄清楚如何(或在哪里)按字母顺序对数组进行排序

<?php

// opens images folder
if ($handle = opendir('Images')) {
while (false !== ($file = readdir($handle))) {

// strips files extensions  
$crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    

$newstring = str_replace($crap, " ", $file );   

//asort($file, SORT_NUMERIC); - doesnt work :(

// hides folders, writes out ul of images and thumbnails from two folders

    if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
    echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";}
}
closedir($handle);
}

?>

非常感谢任何建议或指示!

5 个答案:

答案 0 :(得分:22)

在对文件进行排序之前,您需要先将文件读入数组。怎么样?

<?php
$dirFiles = array();
// opens images folder
if ($handle = opendir('Images')) {
    while (false !== ($file = readdir($handle))) {

        // strips files extensions      
        $crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    

        $newstring = str_replace($crap, " ", $file );   

        //asort($file, SORT_NUMERIC); - doesnt work :(

        // hides folders, writes out ul of images and thumbnails from two folders

        if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
            $dirFiles[] = $file;
        }
    }
    closedir($handle);
}

sort($dirFiles);
foreach($dirFiles as $file)
{
    echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";
}

?>

编辑:这与您提出的问题无关,但您也可以使用pathinfo()功能对文件扩展名进行更通用的处理。您不需要硬编码的扩展数组,您可以删除任何扩展名。

答案 1 :(得分:10)

使用opendir()

opendir()不允许对列表进行排序。您必须手动执行排序。为此,首先将所有文件名添加到数组中,然后使用sort()对其进行排序:

$path = "/path/to/file";

if ($handle = opendir($path)) {
    $files = array();
    while ($files[] = readdir($dir));
    sort($files);
    closedir($handle);
}

然后使用foreach列出它们:

$blacklist = array('.','..','somedir','somefile.php');

foreach ($files as $file) {
    if (!in_array($file, $blacklist)) {
        echo "<li>$file</a>\n <ul class=\"sub\">";
    }
}

使用scandir()

使用scandir()可以轻松实现这一目标。它默认为您执行排序。使用以下代码可以实现相同的功能:

$path = "/path/to/file";
$blacklist = array('somedir','somefile.php');

// get everything except hidden files
$files = preg_grep('/^([^.])/', scandir($path)); 

foreach ($files as $file) {
    if (!in_array($file, $blacklist)) {
        echo "<li>$file</a>\n <ul class=\"sub\">";
    }
}

使用DirectoryIterator(首选)

$path = "/path/to/file";
$blacklist = array('somedir','somefile.php');

foreach (new DirectoryIterator($path) as $fileInfo) {
    if($fileInfo->isDot()) continue;
    $file =  $path.$fileInfo->getFilename();
    echo "<li>$file</a>\n <ul class=\"sub\">";
}

答案 2 :(得分:3)

这就是我这样做的方式

if(!($dp = opendir($def_dir))) die ("Cannot open Directory.");
while($file = readdir($dp))
{
    if($file != '.')
    {
        $uts=filemtime($file).md5($file);  
        $fole_array[$uts] .= $file;
    }
}
closedir($dp);
krsort($fole_array);

foreach ($fole_array as $key => $dir_name) {
  #echo "Key: $key; Value: $dir_name<br />\n";
}

答案 3 :(得分:1)

注意:将其移动到foreach循环中,以便正确重命名新闻字符串变量。

// strips files extensions      
$crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    

$newstring = str_replace($crap, " ", $file );

答案 4 :(得分:0)

$directory = scandir('Images');