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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…