从 Linux 2.6.13 内核开始,Linux 就推出了 inotify,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。

	参考文章:
		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

	简介:
		PHP的inotify扩展,暴露了inotify函数,inotify_init()、inotify_add_watch()、inotify_rm_watch()
		正如C语言的inotify_init()函数,返回一个文件描述符,PHP的inotify_init()返回一个stream流资源,使用标准的流函数,像:stream_select()、stream_set_blocking()和fclose()。PHP的inotify_read()函数替代了C语言读取inotify事件的方式。
	安装:
		未与PHP绑定,可通过:http://pecl.php.net/package/inotify 来下载编译安装

	函数:
		inotify_init() - 初始化一个inotify实例,用于 'inotify_add_watch()'
		inotify_add_watch() - 给inotify添加一个监控文件或目录,返回监控的唯一id
		inotify_rm_watch() - 移除inotify的一个监控
		inotify_read() - 从inotify中,读取inotify事件
		inotify_queue_len() - 如果有等待的事件,返回一个>0的数字。通过该函数,我们可以知道 'inotify_read()' 是否阻塞。如果返回一个>0的数字,有等待事件,inotify_read()将不会阻塞

	手册上示例:
	<?php
		// Open an inotify instance - 实例化
		$fd = inotify_init();

		// Watch __FILE__ for metadata changes (e.g. mtime) - 监听 '__FILE__' 元数据的变化
		$watch_descriptor = inotify_add_watch($fd, __FILE__, IN_ATTRIB);

		// generate an event - 生成一个事件
		touch(__FILE__);

		// Read events - 读取事件
		$events = inotify_read($fd);
		print_r($events);

		// The following methods allows to use inotify functions without blocking on inotify_read(): - 下面的方法允许使用inotify函数,而不会在 'inotify_read()' 阻塞

		// - Using stream_select() on $fd: - 在实例化的inotify上,使用 'stream_select()'
		$read = array($fd);
		$write = null;
		$except = null;
		stream_select($read,$write,$except,0);

		// - Using stream_set_blocking() on $fd - 在实例化的inotify上,使用 'stream_set_blocking()',设置非阻塞
		stream_set_blocking($fd, 0);
		inotify_read($fd); // Does no block, and return false if no events are pending

		// - Using inotify_queue_len() to check if event queue is not empty - 检查事件队列是否为空
		$queue_len = inotify_queue_len($fd); // If > 0, inotify_read() will not block - >0,inotify_read() 将不会阻塞

		// Stop watching __FILE__ for metadata changes - 停止监听 '__FILE__' 元数据的改变
		inotify_rm_watch($fd, $watch_descriptor);

		// Close the inotify instance - 关闭 inotify 实例
		// This may have closed all watches if this was not already done
		fclose($fd);
	?>

	某人给的例子:
	<?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 be 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 was modified
		                case ($evdetails['mask'] & 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;
		                    // be a nice guy and program good code ;-)
		                    break;

		                    // File was moved or deleted
		                case ($evdetails['mask'] & IN_MOVE):
		                case ($evdetails['mask'] & IN_MOVE_SELF):
		                case ($evdetails['mask'] & IN_DELETE):
		                case ($evdetails['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 like that:
		$lastpos = 0;
		while (true) {
		    echo tail($file,$lastpos);
		}
	?>

Logo

更多推荐