PHP用inotify扩展监控文件
可以用inotify扩展
提供的功能来监控文件/目录,实现某些特殊的功能:如热编译,安全预警。
早期phpStudy有提供防挂马功能,就是用了与inotifyf
类似的机制。
inotify
扩展提供了一系列inotify
函数:inotify_init()
inotify_add_watch()
inotify_rm_watch()
。
类似C语言里的inotify_init()
函数会返回文件描述符,PHP的inotify_init()
则返回 stream资源。可以被标准的Stream函数使用,例如 stream_select()
stream_set_blocking()
,以及fclose()
。
1. 安装
$ pecl install inotify
安装后在php.ini
中加上
extension=inotify.so
2. 函数概览
2.1 inotify_init
用于初始化一个inotify
实例。
函数原型:
resource inotify_init(void)
失败则返回FALSE
2.2 inotify_add_watch
添加一个文件或文件夹至监控列表中。如果已经存在,则视为修改。
函数原型:
int inotify_add_watch(resource $inotify_instance, string $pathname, int $mask)
- $inotify_instance
inotify_init()
返回的资源 - $pathname 要监控的文件或文件夹
- $mask 要监控的事件
该函数的返回值是一个唯一的监控描述符。可以在inotify_rm_watch()
中使用,用于移除对它的监控。
2.3 inotify_read
从inotify instance中读取事件。
函数原型:
array i