+3 votes
908 views
How to list contents of a directory sorted by date in php

in Help by (552k points) | 908 views

1 Answer

+4 votes
Best answer

Once we have the problem of listing the contents of a directory or folder in php language in which the names of the files are computer by creation date, well, with this simple code we can obtain it easily. It is based on the creation of an array, sort it by creation date and then display it, using a li or a table, for example

$ dir = "../mydir/";
chdir ($ dir);
array_multisort (array_map ('filemtime', ($ files = glob ("*. *"))), SORT_DESC, $ files);
echo "<table>";
foreach ($ files as $ filename)
{
 if ($ tr == 0)
 {
 echo "<tr>";
 }
 echo "<td> <a href='download.php?path=".$filename."' target='_blank'>" .substr ($ filename, 0, -4). "</a> </ td > ";
 $ tr ++;

 if ($ tr == 4)
 {
 echo "</ tr>";
 $ tr = 0;
 }
 
}
echo "</ table>";

Where download is the call to the script to download the file.


by (552k points)

Related questions

+4 votes
1 answer
+4 votes
1 answer
+3 votes
1 answer
asked Nov 25, 2022 in Guides by backtothefuture (552k points) | 70 views
+3 votes
1 answer
asked Nov 19, 2020 in Help by backtothefuture (552k points) | 271 views
+3 votes
1 answer
asked Nov 19, 2020 in Help by backtothefuture (552k points) | 332 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users