Possible Duplicate:
get last modified file in a dir?
I have lot of file inside a folder. How can I get the last modified file name using php?
Is it possible? kindly help me.Thanks in advance.
Possible Duplicate:
get last modified file in a dir?
I have lot of file inside a folder. How can I get the last modified file name using php?
Is it possible? kindly help me.Thanks in advance.
Yes, it is possible. Please refer this method: http://php.net/manual/en/function.filemtime.php
In order to work list all files in a folder an get the filemtime. Compare them and you will find the latest updated file in that folder.
Remember that, this method will return in UNIX time() but not a date like (YYYY/mm/dd)
UPDATE: Finding most new file is directly is not possible. You have to check files by one by.
<?php
function findMostNewFile($folder) {
$files = array();
foreach (scandir($folder) as $file) {
$path = $folder . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) continue; // do not count folders. only files.
$files[filemtime($path)] = $path;
}
krsort($files);
$arr = array_slice($files, 0, 1); // return the first newest file's path as array
return $arr[0]; // return only a path and name as a string
}
?>
Thats all.
UPDATE: return as string.
I'm pretty sure you'll have to look through all files and grab the max mod time. Use 'glob' to loop through files and use filemtime() to grab the file mod date/time.