read the latest updated csv file from the folder with php

前端 未结 2 1230
一整个雨季
一整个雨季 2021-01-23 19:24

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

Exmaple: I have a folder \'Report\'; user will put in the new c

相关标签:
2条回答
  • 2021-01-23 19:58

    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 讨论(0)
  • 2021-01-23 20:02

    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";
    
    0 讨论(0)
提交回复
热议问题