Listing and sorting files using PHP DirectoryIterator

I am using DirectoryIterator to create a linked list of PDF files. (There is also code to split the file name to make the list more user friendly.) I would like to sort the results by file name, but I can't figure out how to do it. I know that I need to pass the results to an array and then sort the array. But I canโ€™t find an example wherever it is, so I canโ€™t figure out how to integrate the array / sort into my code, since my PHP is weak. Can anyone help?

($ path 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>'; } ?> 
+6
source share
2 answers

Placing your actual files in an array with different columns that you can sort is probably the best option and also associated with it.

I used asort () "Sort array and maintain index association", I think that best suits 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 ); } } } 

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 to print with HTML after the function finishes - your code did this while the code was in the loop, which meant that you could not sort it after it was already printed:

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

Use Scandir instead of DirectoryIterator to get a sorted list of files:

 $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. For case insensitive sorting, see this answer: how do you scan case-insensitive cases?

0
source

Source: https://habr.com/ru/post/952114/


All Articles