Starting with the Linux 2.6.13 kernel, Linux has introduced inotify, allowing the monitor to open a standalone file descriptor and monitor one or more files for the event set, such as opening, closing, moving/renaming, deleting, creating, or changing attributes. Reference article: http://blog.jiunile.com/php%E4%BD%BF%E7%94%A8inotify%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97%E5%A4%84%E7%90% 86.html http://os.51cto.com/art/201011/232694_all.htm http://blog.csdn.net/myarrow/article/details/7096460 Introduction: PHP INotify extensions, exposing the inotify function, Inotify_init (), Inotify_add_watch (), Inotify_rm_watch () as the C-language Inotify_init () function, returns a file descriptor, PHP's Inotify_init () returns a stream stream resource using standard stream functions such as: Stream_select (), stream_set_blocking (), and fclose ().
The Inotify_read () function of PHP replaces the way in which the C language reads INotify events. Installation: not bound to PHP, can be downloaded by: http://pecl.php.net/package/inotify to download the build install function: Inotify_init ()-Initializes a inotify instance for ' INOTIFY_ADD_WATC H () ' Inotify_add_watch ()-Add a monitor file or directory to inotify, return the monitored unique ID inotify_rm_watch ()-Remove inotify from a monitoring inotify_read ()-From Inoti FY, read the INotify event Inotify_queue_len ()-If there is a wait event, return a >0 number. Through this function, we can know if ' inotify_read () ' is blocked. If you return a >0 number, there is a wait event, Inotify_read () will not block the manual example: <?php//Open an INotify instance-instantiated $FD = Inotify_init (); Watch __file__ for metadata changes (e.g. Mtime)-Listens for changes in ' __file__ ' meta data $watch _descriptor = Inotify_add_watch ($FD, __
file__, In_attrib);
Generate an event-generate an incident touch (__file__);
Read Events-reading event $events = Inotify_read ($FD);
Print_r ($events); The following methods allows to using INotify functions without blocking on inotify_read ():-The following method allows the use of the INotify function instead of the ' in
Otify_read () ' blocking//-using Stream_select () on $FD:-On the instantiated inotify, use ' stream_select () ' $read = Array ($FD);
$write = null;
$except = null;
Stream_select ($read, $write, $except, 0);
-Using stream_set_blocking () on $FD-use ' stream_set_blocking () ' On the instantiated inotify, setting non-blocking stream_set_blocking ($FD, 0); Inotify_read ($FD); Does no blocks, and return False if no events are pending//-Using Inotify_queue_len () to check if event \ n OT empty-Checks whether the event queue is empty $queue _len = Inotify_queue_len ($FD); If > 0, Inotify_read () would not BLock->0,inotify_read () will not block//stop watching __file__ for metadata changes-stop listening for changes in ' __file__ ' meta data inotify_rm_
Watch ($FD, $watch _descriptor); Close the INotify instance-inotify instance//This may have closed all watches if this is not already done fclose (
$FD);
?> example given by someone: <?php/** * Tail a file (UNIX only!) * Watch a file for changes using INotify and return the changed data * * @param string $file-filename of the file to is watched * @param integer $pos-actual position in the file * @return string/function tail ($file,& $pos)
{//Get the size of the file if (! $pos) $pos = FileSize ($file);
Open an inotify instance $fd = Inotify_init ();
Watch $file for changes.
$watch _descriptor = Inotify_add_watch ($FD, $file, in_all_events);
Loop forever (Breaks are below) while (true) {//Read events (Inotify_read is blocking!)
$events = Inotify_read ($FD); Loop though the events which occured foreach ($events as $event => $evdetails) {//React on The event type switch (TRUE) {//File is modified case ($evdetails [' ma SK '] & in_modify)://Stop watching $file for changes Inotify_rm_watch ($FD
, $watch _descriptor);
Close the INotify instance fclose ($FD);
Open the file $fp = fopen ($file, ' R ');
if (! $fp) return false;
Seek to the last EOF position fseek ($fp, $pos);
Read until EOF while (!feof ($fp)) {$buf. = Fread ($fp, 8192); //Save the new EOF to $pos $pos = Ftell ($FP);
(Remember: $pos is called by reference) Close the file pointer fclose ($FP);
Return the new data and leave the function return $buf;
Being a nice guy and program good Code;-) break; File is moved or deleted case ($evdetails [' Mask '] & In_move): Case ($evdetails [' Mask '] & in_move_self): Case ($evdetails [' Mask '] & In_delete): Case ($evdeta ils[' mask '] & in_delete_self)://Stop watching $file for changes Inotify_
Rm_watch ($FD, $watch _descriptor);
Close the INotify instance fclose ($FD);
Return a failure return false;
Break
}}//Use it as: $lastpos = 0; while (true) {echo TaiL ($file, $lastpos); }?>