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

in Help by (551k points) | 893 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 (551k points)

Related questions

+4 votes
1 answer
+4 votes
1 answer
+3 votes
1 answer
asked Nov 25, 2022 in Guides by backtothefuture (551k points) | 50 views
+3 votes
1 answer
asked Nov 19, 2020 in Help by backtothefuture (551k points) | 253 views
+3 votes
1 answer
asked Nov 19, 2020 in Help by backtothefuture (551k points) | 303 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,632 questions
10,764 answers
510 comments
3 users