1<?php 2/* 3The inotify extension exposes the inotify functions inotify_init(), 4inotify_add_watch() and inotify_rm_watch(), which work just like the C 5ones. 6 7As the C inotify_init() function returns a file descriptor, PHP's 8inotify_init() returns a stream resource, so you can use the standard 9stream functions on it, like stream_select(), stream_set_blocking(), 10and also fclose(), used to close an inotify instance and all its 11watches. 12 13It also exports two additional functions: 14 15inotify_queue_len(resource $inotify_instance), which returns a number 16upper than zero if their are waiting events 17 18inotify_read(resource $inotify_instance), which replaces the C way 19of reading inotify events. It returns arrays of events, in which keys 20represents the members of the inotify_event structure. 21*/ 22 23// Open an inotify instance 24$fd = inotify_init(); 25 26// Watch __FILE__ for metadata changes (e.g. mtime) 27$watch_descriptor = inotify_add_watch($fd, __FILE__, IN_ATTRIB); 28 29// generate an event 30touch(__FILE__); 31 32// Read events 33$events = inotify_read($fd); 34// it may return something like this: 35array( 36 array( 37 'wd' => 1, // $watch_descriptor 38 'mask' => 4, // IN_ATTRIB bit is set 39 'cookie' => 0, // unique id to connect related events (e.g. 40 // IN_MOVE_FROM and IN_MOVE_TO events) 41 'name' => '', // the name of a file (e.g. if we monitored changes 42 // in a directory) 43 ), 44); 45print_r($events); 46 47// If we do not want to block on inotify_read(), we may 48 49// - Use stream_select() on $fd: 50$read = array($fd); 51$write = null; 52$except = null; 53stream_select($read,$write,$except,0); 54 55// - Use stream_set_blocking() on $fd 56stream_set_blocking($fd, 0); 57inotify_read($fd); // Does no block, and return false if no events are pending 58 59// - Use inotify_queue_len() to check if event queue is not empty 60$queue_len = inotify_queue_len($fd); // If > 0, we can be assured that 61 // inotify_read() will not block 62 63// We do not need anymore to watch events, closing them 64 65// Stop watching __FILE__ for metadata changes 66inotify_rm_watch($fd, $watch_descriptor); 67 68// Close our inotify instance 69// This may have closed all watches if we haven't already closed them 70fclose($fd); 71 72?> 73