使用PHP DirectoryIterator列出和排序文件 [英] List and Sort Files with PHP DirectoryIterator

查看:96
本文介绍了使用PHP DirectoryIterator列出和排序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DirectoryIterator生成PDF文件的链接列表. (也有一些代码可以分解文件名,以使列表更易于使用.)我想按文件名对结果进行排序,但不知道如何进行.我知道我需要将结果放入数组中,然后对数组进行排序.但是,由于我的PHP很弱,所以在任何地方都找不到像我一样的示例,因此我无法弄清楚如何将数组/排序集成到我的代码中.谁能帮忙?

I'm using DirectoryIterator to generate a linked-list of PDF files. (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. Can anyone lend an assist?

($ path在页面的其他位置声明)

($path is declared elsewhere on the page)

<?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.

我使用了 asort()"对数组进行排序并维护索引关联",我认为这最符合您的要求.

I used asort() "Sort an array and maintain index association" with I think best fits your requirements.

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

之后

After Sorting 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进行打印-您的代码在代码处于循环状态时进行了打印,这意味着在打印后无法对其进行排序:

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:

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

这篇关于使用PHP DirectoryIterator列出和排序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆