-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_iterator.php
More file actions
37 lines (29 loc) · 1.2 KB
/
file_iterator.php
File metadata and controls
37 lines (29 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//////////////////////////////////////////////////////////////////////
// //
// This function uses the RecursiveDirectoryIterator class //
// to iterate through every pdf file in a directory //
// and returns the most recent file by modified date //
// Author: Anthony George //
// //
//////////////////////////////////////////////////////////////////////
<?php
function latestPDF($filepath){
$iterator = new RecursiveDirectoryIterator($filepath);
$lastModified = "";
foreach ($iterator as $file) {
if ($file->isFile()) {
if(empty($lastModified)){
$lastModified = $file;
} else {
$ext = pathinfo($file, PATHINFO_EXTENSION);
$date1 = filemtime($lastModified);
$date2 = filemtime($file);
if(($date1 < $date2) && ($ext == 'pdf')) {
$lastModified = $file;
}}}}
if(empty($lastModified)){
throw new exception("No file");
}
return $lastModified;
}
?>