Question:

Reverse this PHP code to do opposite?

by  |  earlier

0 LIKES UnLike

http://www.weberdev.com/get_example-4050.html

Thats the link to the code, is there a way i can get it to return the newest file instead of the oldest??

 Tags:

   Report

1 ANSWERS


  1. Easy.

    - - - - - - - - - - - - - - - - - - - - - - - - -

    <?php

    function get_newest_file($directory) {

        if ($handle = opendir($directory)) {

            while (false !== ($file = readdir($handle))) {

                $files[] = $file;

            }

            foreach ($files as $val) {

                if (is_file($directory.$val)) {

                    $file_date[$val] = filemtime($directory.$val);

                }

            }

        }

        closedir($handle);

        arsort($file_date, SORT_NUMERIC);

        reset($file_date);

        $newest = key($file_date);

        return $newest;

    }

    // example:

    echo get_newest_file("the_way/to_the/director...

    ?>

    - - - - - - - - - - - - - - - - - - - - - - - - -

    The only difference is where it originally had "asort" it now reads "arsort" which is the opposite.

    Hope this helps. Stevie.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.