1

Is it possible to detect the modification of a file in PHP?

Particularly I'm looking for when the file is updated, but create/save/ etc. would be nice.

Ultimately, I'm hoping to trigger some sort of action on my server for sending an email alert or something similar when a file is updated.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Jay
  • 187
  • 1
  • 2
  • 11

4 Answers4

1

filemtime should do what you want

Salman A
  • 262,204
  • 82
  • 430
  • 521
overloading
  • 1,210
  • 4
  • 25
  • 46
  • This doesn't really answer the question. `filemtime` will allow you to see the modified time of a file, but not "detect" a new modification / new file. You'd need to tie it in with something like a cron job. – Jonnix Jan 09 '13 at 21:14
  • Thanks this was helpful. Browsing through the user contributed notes for the filemtime func, I found something I may be able to run on a cron to get what I want. – Jay Jan 09 '13 at 21:23
  • @JonStirling For every solution (`filemtime`, or `md5_file()`) you need to keep a previous state to detect the changes. – KingCrunch Jan 09 '13 at 21:24
  • @KingCrunch Not really. With `filemtime` and When using cron you know how often it runs, as such you would be looking for changes made within the difference of time between the start of 2 runs. No need to store anything. – Jonnix Jan 09 '13 at 21:28
  • @JonStirling Would tell it an "implicit state" ;), but right, this way you need a storage to track the latest change. – KingCrunch Jan 09 '13 at 21:30
  • @KingCrunch Aye. But, if you were commenting on my first comment, when I said detect I meant it in the context of the OP, as in `filemtime` (or PHP for that matter) won't actively monitor the file system for changes, thus won't work for triggers, which is what the OP was about. – Jonnix Jan 09 '13 at 21:34
  • Yeah, I'm sorry, I should have been more clear initially. Was hoping for the active detection of the modification, instead of having to check for modification manually via cron or something. Appreciate all the help! – Jay Jan 09 '13 at 21:38
1

For a cron-job-less approach, you can have a look at ext/inotify. There you register a callback and put the whole process into a read-loop, that always triggers the callback, if an event (for example IN_CLOSE_WRITE) occurs.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
0

See filemtime. There are similar functions to check the creation time etc.

Salman A
  • 262,204
  • 82
  • 430
  • 521
asmecher
  • 1,055
  • 7
  • 12
0

If you want to check wether the file was changed, you could record the md5 hash of the files:

http://php.net/manual/en/function.md5-file.php

Here is the same question: PHP folder watching on windows

With ruby, python or other languages, you could register a watcher on the filesystem, so you have a push and no information pull. But afaik is this not possible with PHP.

Community
  • 1
  • 1
Fabian Blechschmidt
  • 4,113
  • 22
  • 39
  • Yeah, I see what you're saying and was hoping for more of a push method myself. Appreciate the help though, I'll take a look at maybe setting up a python script. – Jay Jan 09 '13 at 21:21