簡體   English   中英

使用PHP DirectoryIterator列出和排序文件

[英]List and Sort Files with PHP DirectoryIterator

我正在使用DirectoryIterator生成PDF文件的鏈接列表。 (還有一些代碼來分解文件名以使列表更加用戶友好。)我想按文件名對結果進行排序,但無法弄清楚如何。 我知道我需要將結果放入數組中,然后對數組進行排序。 但是,我無法在任何地方找到一個像我一樣的例子,所以我無法弄清楚如何將數組/排序集成到我的代碼中,因為我的PHP很弱。 有人可以借助嗎?

($ 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>';
          }

        ?>

將您的有效文件放入一個包含各種列的數組中,您可以將其排序,這可能是最好的選擇,也可以是相關聯的。

我使用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
                     );       
       }
    }
 }

排序前

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
       ) 
)

排序后的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
        )
 )

}

然后在函數完成后使用HTML進行打印 - 您的代碼在代碼處於循環中時執行了此操作,這意味着您無法在已經打印之后對其進行排序:

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

使用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區分大小寫。 對於不區分大小寫的排序,請參閱以下答案: 如何掃描不區分大小寫的目錄?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM