Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
921 views
in Technique[技术] by (71.8m points)

sorting - List and Sort Files with PHP DirectoryIterator

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

        ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

if (is_dir($path)) 
{
    $FoundFiles = [];

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

       if ($file->isDot())
       {
          continue;
       }
    
       $fileName = $file->getFilename();

       $pieces    = explode('.', $fileName);
       $date      = explode('-', $pieces[2]);
                
       $filetypes = [ "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()

/** 
 *   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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...