开发者

read the latest updated csv file from the folder with php

开发者 https://www.devze.com 2023-02-05 12:45 出处:网络
Just wandering, will it be possible to make the system always read the latest updated csv file with php??

Just wandering, will it be possible to make the system always read the latest updated csv file with php??

Exmaple: I have a开发者_如何学Python folder 'Report'; user will put in the new csv file (with difference file's name) into this folder anytime when the report is ready. Any way that I can do the coding in php, to make it only read the latest csv file found in the Report folder (instead of reading the csv file based on the hard coded file name in php)?


This should fine the file with the "highest" modification time (meaning the last modified file). You can use filemtime for this.

$dir = "dir/to/check/";
$dh = opendir($dir);
$last = 0;
$name = "";
while (($file = readdir($dh)) !== false){
    if(is_file($dir.$file)){
        $mt = filemtime($dir.$file);
        if($mt > $last){
            $last = $mt;
            $name = $file;
        }
    }
}
closedir($dh);

echo "Last modified file: $name";


You can use the filemtime() function in php. Here is the manual page:

http://php.net/manual/en/function.filemtime.php

You just need to get a list of a all files in the directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消