php -- inotify 监控

官方文档:https://www.php.net/manual/zh/function.inotify-init.php
可以用来实现监控文件的变化

主要方法:
inotify_init() - 初始化实例
inotify_add_watch() - 添加一个监听实例
inotify_rm_watch() - 删除一个监听实例
inotify_queue_len() - 返回一个监听队列长度
inotify_read() - 读去监听实例

其中:
inotify_add_watch ( resource $inotify_instance , string $pathname , int (mask ) : int )inotify_instance :初始化实例
(pathname :监听文件的路径,这一个一定得是文件路径,不能是目录路径 )mask:系统预定义的常量,用于标示是监听文件的触发动作,是创建,修改、删除等时候触发

且仅在此扩展编译入 PHP 或在运行时动态载入时可用。

Inotify constants usable with inotify_add_watch() and/or returned by inotify_read()
IN_ACCESS (int)
File was accessed (read) () 访问文件
IN_MODIFY (int)
File was modified (
) 文件被修改
IN_ATTRIB (int)
Metadata changed (e.g. permissions, mtime, etc.) ()
IN_CLOSE_WRITE (int)
File opened for writing was closed (
) 文件编写时关闭
IN_CLOSE_NOWRITE (int)
File not opened for writing was closed () 文件未编写关闭
IN_OPEN (int)
File was opened (
) 文件被打开
IN_MOVED_TO (int)
File moved into watched directory () 文件移动
IN_MOVED_FROM (int)
File moved out of watched directory (
) 文件移出
IN_CREATE (int)
File or directory created in watched directory () 文件/目录 创建
IN_DELETE (int)
File or directory deleted in watched directory (
) 文件/目录 删除
IN_DELETE_SELF (int)
Watched file or directory was deleted 文件/目录被删除
IN_MOVE_SELF (int)
Watch file or directory was moved 文件/目录移动
IN_CLOSE (int)
Equals to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
IN_MOVE (int)
Equals to IN_MOVED_FROM | IN_MOVED_TO
IN_ALL_EVENTS (int)
Bitmask of all the above constants
IN_UNMOUNT (int)
File system containing watched object was unmounted
IN_Q_OVERFLOW (int)
Event queue overflowed (wd is -1 for this event)
IN_IGNORED (int)
Watch was removed (explicitly by inotify_rm_watch() or because file was removed or filesystem unmounted
IN_ISDIR (int)
Subject of this event is a directory
IN_ONLYDIR (int)
Only watch pathname if it is a directory (Since Linux 2.6.15)
IN_DONT_FOLLOW (int)
Do not dereference pathname if it is a symlink (Since Linux 2.6.15)
IN_MASK_ADD (int)
Add events to watch mask for this pathname if it already exists (instead of replacing mask).
IN_ONESHOT (int)
Monitor pathname for one event, then remove from watch list.
Note: The events marked with an asterisk (*) above can occur for files in watched directories.


// 创建监听实例
$fd = inotify_init();

$testFile = __DIR__. '/test.text';

// 监听文件
inotify_add_watch($fd, $testFile, IN_MODIFY);
print_r($testFile);

// Read events
while (true) {
    $events = inotify_read($fd);
    print_r($events);
}
/var/www/html/13/test.textArray
(
    [0] => Array
        (
            [wd] => 1
            [mask] => 32768
            [cookie] => 0
            [name] =>
        )

)

当我修改文件时,就会接收到监听实例的返回

原文地址:https://www.cnblogs.com/smallyi/p/14058187.html