Reputation: 15032
Strangely when I tried to google this question entering queries similar to this question nothing useful popped out, not even close.
Now I am not looking for a solution where you scan or open directory and read its contents file by file. For those looking for such inreliable loop, checkout these functions: scandir(...)
to load all at once and then loop it, and opendir(...)
, readdir(...)
, stat(...)['mtime']
to do it file by file and possibly setting some limit on how many is done and make it at least a bit safer...
What I would like to know is whether there is something in PHP that could check/test, in some simple way, whether a folder/directory contains file older than [sometime], or even how many of such files are there without any kind of "manual" go-through?
PS: I've seen some shell_exec
s but that does not seem quite cross-OS...
Upvotes: 3
Views: 389
Reputation: 27854
Obviously there is no built-in function to do that, as this is something too specific. You will have to do it yourself one way or another, just like those shell functions you like so much are doing internally.
Write a function that does that so you can use it where its needed. And naturally PHP offers a bunch of functions that make the whole thing smaller and potentially faster. You can for instance do this:
function folder_has_older_file($folder, $time)
{
return count(array_filter(array_map('filemtime', glob("$folder/*")), function ($a) use ($time) { return $a < $time; })) > 0;
}
Upvotes: 1
Reputation: 2794
You can use filectime(path)
or filemtime(path)
function to check creation time from file or folder. It will return unixtime
, then you can compare with sometime
Upvotes: 0