read the latest updated csv file from the folder 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.
精彩评论