Reputation: 331
I have used the following script to correctly display ALL files from the selected directory and its sub-directories. Does anyone know how to modify this code only echo the most recent file across the directory/subdirectories?
function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } foreach (ListFiles('media/com_form2content/documents/c30') as $key=>$file){ echo "{aridoc engine=\"google\" width=\"750\" height=\"900\"}" . $file ."{/aridoc}"; }
Upvotes: 2
Views: 7324
Reputation: 404
I would recommend you use the filemtime()
function.
This will give you the files last modified date.
Upvotes: 0
Reputation: 736
you can use this to get last addition file in directory
$path = "/path/to/my/dir";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
}
Upvotes: 1
Reputation: 2750
you can try this
$last_mtimes = array();
function ListFiles($dir) {
if($dh = opendir($dir)) {
$files = Array();
$inner_files = Array();
while($file = readdir($dh)) {
if($file != "." && $file != ".." && $file[0] != '.') {
if(is_dir($dir . "/" . $file)) {
$inner_files = ListFiles($dir . "/" . $file);
if(is_array($inner_files)) $files = array_merge($files, $inner_files);
} else {
array_push($files, $dir . "/" . $file);
$lmtime = filemtime($dir . "/" . $file) ;
$last_mtimes[$lmtime] = $dir . "/" . $file;
}
}
}
// now ksort your $last_mtimes array
krsort($last_mtimes);
// either return this array or do whatever with the first val
closedir($dh);
return ($last_mtimes);
}
}
// prints in decsending order
foreach (ListFiles('PATH_TO_YOUR_DIRECTORY') as $key=>$file){
echo "{aridoc engine=\"google\" width=\"750\" height=\"900\"}" . $key."=>".$file ." {/aridoc}";
}
// prints last modified files
echo array_shift(ListFiles('YOUR_DIRECTORY_PATH'));
hope this helps
Upvotes: 0
Reputation: 20394
In PHP5
you can use RecursiveDirectoryIterator
to recursively scan all files in a directory:
$mostRecentFilePath = "";
$mostRecentFileMTime = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("YOURDIR"), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
if ($fileinfo->getMTime() > $mostRecentFileMTime) {
$mostRecentFileMTime = $fileinfo->getMTime();
$mostRecentFilePath = $fileinfo->getPathname();
}
}
}
Upvotes: 9
Reputation: 6718
You can use filemtime()
to retrieve file's last modified unix timestamp.
Upvotes: 2