3

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_execs but that does not seem quite cross-OS...

jave.web
  • 13,880
  • 12
  • 91
  • 125
  • The manual contains a comment from "paranoid at dds dot nl" that may be relevant to your needs: http://php.net/manual/en/function.filemtime.php – markdwhite Dec 01 '16 at 04:21
  • @markdwhite that is really interesting however this gets almost the opposite of what I want - the newest file change, not the oldest - that would be enaugh for me :-) – jave.web Dec 01 '16 at 05:13
  • @jave.web so you're looking for a standard platform-independent PHP command that will scan a directory in a fancy way w/o you making that manually and w/o a loop; and so it should return "true" OR "false" if there is at least one file created before a certain date OR "number" of such files? No. There is no such a function. Your limitation "cross-OS" leads to the only answer "go through the files in a loop". You can check an example here: http://stackoverflow.com/a/2205784/4621324 – Axalix Dec 01 '16 at 05:39
  • If you don't want to see an explicit loop in your code, this is another approach: http://stackoverflow.com/a/3298787/4621324 – Axalix Dec 01 '16 at 06:17
  • @Axalix OK, thanks for the info, anyway, it's not about explicit loop, it's about not having to go through all, possibly maaaany, files. And are there at least OS-cross solutions for Unix, Linux, Windows ? (command that would work on all production used Windows, another one that would work for all Linux ... I think you get the point?) – jave.web Dec 01 '16 at 07:17

2 Answers2

1

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;
}
Havenard
  • 27,022
  • 5
  • 36
  • 62
  • I see... Note: glob is basically conditioned scandir - so that is not what I said I am looking for. However what is interesting is the idea doing it by functions that might be optimized for performance (if not now, there is possibility of it happening in future). So I will not accept it as answer, but give it a thumbs up for the idea ;) – jave.web Dec 02 '16 at 19:19
  • 1
    @jave.web It will always scan the directory at some level, there is no way around it. – Havenard Dec 03 '16 at 03:50
  • that is sad, but if so, I would rather for it to be done directly on the system level (though called through PHP). – jave.web Dec 03 '16 at 17:32
0

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

Dolly Aswin
  • 2,684
  • 1
  • 20
  • 23
  • It would still require to loop through files to get the answer - as I said in **bold** - that is not what I am looking for, because I know how to do that :-) – jave.web Dec 01 '16 at 05:17