Question:

List all HTML files in directory from newest to oldest in PHP?

by  |  earlier

0 LIKES UnLike

How would i do that? I can do it normally without ordering by time but i cant figure this part out. (Newest at the top, oldest at the bottom).

 Tags:

   Report

2 ANSWERS


  1. you can call the function filemtime(filename) to get the time the file was last modified.  go through your files and then you can sort them.  below is some code i use to sort some files by last modified date.  The files are named 1.php, 2.php, 3.php... etc (but 2 may be modified more recently than 1 or 3, just depends.)

    $number=1;

    while (file_exists($number . ".php")) {

    $array["number"][$number] = $number;

    $array["mod"][$number] = date("YmdHis", filemtime($number . ".php"));

    $number += 1;

    }

    array_multisort($array["mod"], SORT_NUMERIC, SORT_DESC, $array["number"], SORT_NUMERIC, SORT_DESC);

    [EDIT]

    to display...

    foreach ($array["number"] as $number) {

    $file = $number . ".php";

    ?>

    <a href="<?echo $file;?>"><?echo $file?></a>

    <?

    }

    instead of using numbers for file names, you can modify the for each.  i just found it easiest, and for what i needed to do fine, with 1.php, 2.php...

    [/EDIT]


  2. Real simple way of doing this..

    <?php

    $d = dir("/etc/php5");

    echo "Handle: " . $d->handle . "\n";

    echo "Path: " . $d->path . "\n";

    while (false !== ($entry = $d->read())) {

       echo $entry."\n";

    }

    $d->close();

    ?>

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.