How to get the newest file in a directory in php

后端 未结 1 1321
面向向阳花
面向向阳花 2020-11-30 07:07

So I have this app that processes CSV files. I have a line of code to load the file.

$myFile = \"data/FrontlineSMS_Message_Export_20120721.csv\";  //The name         


        
相关标签:
1条回答
  • 2020-11-30 07:57

    Here's an attempt using scandir, assuming the only files in the directory have timestamped filenames:

    $files = scandir('data', SCANDIR_SORT_DESCENDING);
    $newest_file = $files[0];
    

    We first list all files in the directory in descending order, then, whichever one is first in that list has the "greatest" filename — and therefore the greatest timestamp value — and is therefore the newest.

    Note that scandir was added in PHP 5, but its documentation page shows how to implement that behavior in PHP 4.

    0 讨论(0)
提交回复
热议问题