【问题标题】:How to get names of files?如何获取文件名?
【发布时间】:2011-05-11 03:06:24
【问题描述】:

比如,我们有文件夹/images/,里面有一些文件。

还有脚本/scripts/listing.php

我们如何获取文件夹/images/listing.php 中所有文件的名称?

谢谢。

【问题讨论】:

    标签: php file directory


    【解决方案1】:
    <?php
    
    if ($handle = opendir('/path/to/files')) {
        echo "Directory handle: $handle\n";
        echo "Files:\n";
    
        /* This is the correct way to loop over the directory. */
        while (false !== ($file = readdir($handle))) {
            echo "$file\n";
        }
    
        /* This is the WRONG way to loop over the directory. */
        while ($file = readdir($handle)) {
            echo "$file\n";
        }
    
        closedir($handle);
    }
    ?>
    

    见:readdir()

    【讨论】:

      【解决方案2】:

      比 readdir() 更简单,使用 glob:

      $files = glob('/path/to/files/*');
      

      更多信息glob

      【讨论】:

        【解决方案3】:

        使用scandirdir 使这个问题变得微不足道。要在索引从0 开始的数组中获取除特殊文件... 之外的目录中的所有文件,可以将scandirarray_diffarray_merge 结合使用:

        $files = array_merge(array_diff(scandir($dir), Array('.','..')));
        // $files now contains the filenames of every file in the directory $dir
        

        【讨论】:

          【解决方案4】:

          这是一个使用 SPL DirectoryIterator 类的方法:

          <?php
          
          foreach (new DirectoryIterator('../images') as $fileInfo) 
          {
              if($fileInfo->isDot()) continue;
              echo $fileInfo->getFilename() . "<br>\n";
          }
          
          ?>
          

          【讨论】:

            【解决方案5】:

            只是扩展 Enrico 的帖子,还有一些您需要做的检查/修改。

            class Directory
            {
                private $path;
                public function __construct($path)
                {
                    $path = $path;
                }
            
                public function getFiles($recursive = false,$subpath = false)
                {
                    $files = array();
                    $path = $subpath ? $subpath : $this->path;
            
                    if(false != ($handle = opendir($path))
                    {
                        while (false !== ($file = readdir($handle)))
                        {
                            if($recursive && is_dir($file) && $file != '.' && $file != '..')
                            {
                                array_merge($files,$this->getFiles(true,$file));
                            }else
                            {
                                $files[] = $path . $file;
                            }
                        }
                    }
                    return $files;
                }
            }
            

            还有这样的用法:

            <?php
            $directory = new Directory("/");
            $Files = $directory->getFiles(true);
            ?>
            

            这将为您提供如下列表:

            /index.php
            /includes/functions.php
            /includes/.htaccess
            //...
            

            希望这会有所帮助。

            【讨论】:

            • 为什么不直接使用内置的 RecursiveDirectoryIterator / DirectoryIterator...?
            • 信不信由你,但有些人仍在使用 PHP4.x
            猜你喜欢
            • 2015-03-26
            • 2017-07-18
            • 1970-01-01
            • 2014-04-14
            • 2015-08-18
            • 2011-09-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多