简体   繁体   English

使用PHP DirectoryIterator列出和排序文件

[英]List and Sort Files with PHP DirectoryIterator

I'm using DirectoryIterator to generate a linked-list of PDF files. 我正在使用DirectoryIterator生成PDF文件的链接列表。 (There's also some code to break up the file name to make the list more user friendly.) I'd like to sort the results by file name, but can't figure out how. (还有一些代码来分解文件名以使列表更加用户友好。)我想按文件名对结果进行排序,但无法弄清楚如何。 I know I need to put the results into an array, and then sort the array. 我知道我需要将结果放入数组中,然后对数组进行排序。 But, I can't find an example anywhere that does it quite like I am, so I can't figure out how to integrate the array/sort into my code, since my PHP is weak. 但是,我无法在任何地方找到一个像我一样的例子,所以我无法弄清楚如何将数组/排序集成到我的代码中,因为我的PHP很弱。 Can anyone lend an assist? 有人可以借助吗?

($path is declared elsewhere on the page) ($ path在页面的其他地方声明)

<?php
        if (is_dir($path))
          {
            print '<ul>';
            foreach (new DirectoryIterator($path) as $file)
              {
                if ($file->isDot()) continue;
                $fileName = $file->getFilename();
                $pieces = explode('.', $fileName);
                $date = explode('-', $pieces[2]);
                $filetypes = array(
                    "pdf",
                    "PDF"
                );
                $filetype = pathinfo($file, PATHINFO_EXTENSION);
                if (in_array(strtolower($filetype), $filetypes))
                  {
                    print '<li><a href="' . $path . '' . $fileName . '">' . $pieces[2] . '</a></li>';
                  }
              }
             print '</ul>';
          }
        else
          {
            print $namePreferred . ' are not ready.</p>';
          }

        ?>

Placing your valid files into an array with various columns you can sort by is probably the best bet, an associate one at that too. 将您的有效文件放入一个包含各种列的数组中,您可以将其排序,这可能是最好的选择,也可以是相关联的。

I used asort() " Sort an array and maintain index association " with I think best fits your requirements. 我使用asort()排序数组并保持索引关联 ”,我认为最符合您的要求。

if (is_dir($path)) {

    $FoundFiles = array();

    foreach (new DirectoryIterator($path) as $file) {

       if ($file->isDot()) 
          continue;

       $fileName = $file->getFilename();

       $pieces = explode('.', $fileName);
       $date = explode('-', $pieces[2]);

       $filetypes = array(
                    "pdf",
                    "PDF"
                );

       $filetype = pathinfo($file, PATHINFO_EXTENSION);
       if ( in_array( strtolower( $filetype ), $filetypes )) {

          /** 
           *  Place into an Array
          **/
          $FoundFiles[] = array( 
                        "fileName" => $fileName,
                        "date"     => $date
                     );       
       }
    }
 }

Before Sorting 排序前

print_r( $FoundFiles );

Array
(
   [0] => Array
       (
            [fileName] => readme.pdf
            [date] => 22/01/23
       )

   [1] => Array
       (
            [fileName] => zibra.pdf
            [date] => 22/01/53
       )

    [2] => Array
       (
            [fileName] => animate.pdf
            [date] => 22/01/53
       ) 
)

After Sorting asort() 排序后的asort()

/** 
 *   Sort the Array by FileName (The first key)
 *   We'll be using asort()
**/ 
asort( $FoundFiles );

/** 
 *   After Sorting 
**/ 
print_r( $FoundFiles );

Array
(
    [2] => Array
        (
            [fileName] => animate.pdf
            [date] => 22/01/53
        )

    [0] => Array
        (
            [fileName] => readme.pdf
            [date] => 22/01/23
        )

    [1] => Array
        (
            [fileName] => zibra.pdf
            [date] => 22/01/53
        )
 )

} }

Then for printing with HTML after the function completes - Your code did it whilst the code was in a loop, which meant you couldn't sort it after it's already been printed: 然后在函数完成后使用HTML进行打印 - 您的代码在代码处于循环中时执行了此操作,这意味着您无法在已经打印之后对其进行排序:

<ul>
   <?php foreach( $FoundFiles as $File ): ?>
      <li>File: <?php echo $File["fileName"] ?> - Date Uploaded: <?php echo $File["date"]; ?></li>
   <?php endforeach; ?>
</ul>

Use scandir instead of DirectoryIterator to get a sorted list of files: 使用scandir而不是DirectoryIterator来获取已排序的文件列表:

$path = "../images/";

foreach (scandir($path) as $file){

    if($file[0] == '.'){continue;}//hidden file

    if( is_file($path.$file) ){

    }else if( is_dir($path.$file) ){

    }
}

scandir is case sensitive. scandir区分大小写。 For case insensitive sorting, see this answer: How do you scan the directory case insensitive? 对于不区分大小写的排序,请参阅以下答案: 如何扫描不区分大小写的目录?

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

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