【问题标题】:How can I list all files in a directory sorted alphabetically using PHP?如何使用 PHP 按字母顺序列出目录中的所有文件?
【发布时间】:2010-10-20 11:57:07
【问题描述】:

我正在使用以下 PHP 代码列出当前目录下的所有文件和文件夹:

<?php
    $dirname = ".";
    $dir = opendir($dirname);

    while(false != ($file = readdir($dir)))
        {
          if(($file != ".") and ($file != "..") and ($file != "index.php"))
             {
              echo("<a href='$file'>$file</a> <br />");
        }
    }
?>

问题是列表没有按字母顺序排列(也许它是按创建日期排序的?我不确定)。

如何确保它按字母顺序排序

【问题讨论】:

    标签: php sorting directory readdir opendir


    【解决方案1】:

    manual 明确表示:

    读取目录
    返回目录中下一个文件的文件名。文件名按文件系统存储它们的顺序返回。

    您可以做的是将文件存储在一个数组中,对其进行排序,然后将其内容打印为:

    $files = array();
    $dir = opendir('.'); // open the cwd..also do an err check.
    while(false != ($file = readdir($dir))) {
            if(($file != ".") and ($file != "..") and ($file != "index.php")) {
                    $files[] = $file; // put in array.
            }   
    }
    
    natsort($files); // sort.
    
    // print.
    foreach($files as $file) {
            echo("<a href='$file'>$file</a> <br />\n");
    }
    

    【讨论】:

    • 这是我第一次使用PHP。我只需要它来列出我上传到我的 apache 的一些东西。您能告诉我如何将文件存储在数组中并对其进行排序吗?
    • +1 for natsort() 甚至可能想考虑 natcasesort()
    • @David 你可以使用$files = glob("/your/path/*");
    • @David B:顺便说一句:文件名可以包含 HTML 元字符。所以一个人应该在输出之前逃避它们。此外,您还想检查正在上传的文件的有效性。不是说可以上传php文件左右,然后执行..
    • @Pekka:刚刚验证了glob 提供了 sorted 输出。 +1 给你。
    【解决方案2】:
    <?php
    function getFiles(){
        $files=array();
        if($dir=opendir('.')){
            while($file=readdir($dir)){
                if($file!='.' && $file!='..' && $file!=basename(__FILE__)){
                    $files[]=$file;
                }   
            }
            closedir($dir);
        }
        natsort($files); //sort
        return $files;
    }
    ?>
    
    <html>
    <head>
    </head>
    <body>
    
    <h1> List of files </h1>
    
    <ul class="dir">
        <? foreach(getFiles() as $file)
            echo "<li name='$file'><a href='$file'>$file</a></li>";
        ?>
    </ul>
    
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      您可以将所有目录名称放在一个数组中,例如:

      $array[] = $file; 
      

      之后,您可以使用以下命令对数组进行排序:

      sort($array); 
      

      然后打印包含该内容的链接。

      希望对你有所帮助。

      【讨论】:

        【解决方案4】:
        <?php
        $dirname = ".";
        $dir = opendir($dirname);
        
        while(false != ($file = readdir($dir)))
        {
        if(($file != ".") and ($file != "..") and ($file != "index.php"))
        {
          $list[] = $file;
        }
        }
        
        sort($list);
        
        foreach($list as $item) {
        echo("<a href='$item'>$item</a> <br />");
        }
        ?>
        

        【讨论】:

          【解决方案5】:

          使用globsort 应该可以工作。

          【讨论】:

          • 如果使用 glob() 则无需 sort(),因为它默认按字母顺序排序
          【解决方案6】:

          我建议不要使用旧的 opendir()/readdir()。使用 glob() 或者如果您在目录中遇到大量文件,则使用 DirectoryIterator Class(es):

          http://www.php.net/manual/en/class.directoryiterator.php http://www.php.net/manual/en/function.glob.php

          问候

          【讨论】:

          • Jan,不使用 openddir/readdir 的原因是什么?两者都没有被弃用。
          • 因为 OOP 是要走的路。迭代器类也有很多内置到 php 中的特性,当你使用 opendir() 时,你必须手动编码。因此,出于性能或安全原因,我相信这也会更好。
          【解决方案7】:

          【讨论】:

          • 仅链接的答案容易损坏。所有答案都应包含他们的建议作为静态文本。添加链接是支持性的,但需要更多。
          猜你喜欢
          • 2011-04-28
          • 2015-09-12
          • 2015-02-02
          • 2018-06-23
          • 1970-01-01
          • 2011-05-03
          • 2011-09-08
          • 2013-06-03
          • 2010-10-27
          相关资源
          最近更新 更多