监听文件夹和文件(更改) [英] Listening folders and files (changes)

查看:424
本文介绍了监听文件夹和文件(更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以直接在PHP或Node上侦听文件夹和文件的更改(按事件),或者我需要使用自己的方法来做到这一点?

It's possible to listen folders and files changes (by events) on PHP or Node directly, or I need make my own method to do it?

示例:我需要收听文件夹/user.如果我将一些文件添加到该目录中,则PHP或Node会接收到该信息并例如运行PathEvent::fileAdded("/user/user.profile").如果我重命名文件夹,它将运行PathEvent::pathRenamed("/user/save1", "/user/save2").

Example: I need to listen the folder /user. If I add some file into this directory the PHP or Node receives the information and run PathEvent::fileAdded("/user/user.profile") for instance. If I rename a folder it run PathEvent::pathRenamed("/user/save1", "/user/save2").

我知道PHP没有像Node这样的事件系统.但是对于PHP,例如,我可以运行一个方法(目前尚不知道),该方法具有自上次检查以来发生的更改.

I know that PHP don't have an event system like Node. But for PHP I can, for instance, run a method (that I don't know currently) that have the changes ocurred since last time checked.

好吧...我只需要一种开始搜索的方式,我不知道搜索的确切含义是什么.如果您可以举一个例子,那就太好了! :P

Well... I need only a way to start searching, I don't know exactly what is the term of this search. If you can show me an example, it'll be great too! :P

推荐答案

Node.js提供了此功能.您可以在此处阅读.

Node.js provides this functionality. You can read bout this here.

简单的例子:

var fs = require('fs');

fs.watch('somedir', function (event, filename) {
    console.log(event);
    console.log(filename);
});

注意:

观看目录时,并非在所有平台上都支持在回调中提供filename参数(当前仅在Linux和Windows上受支持).即使在受支持的平台上,也不总是保证提供文件名.因此,请不要假设在回调函数中始终提供filename参数,并且如果它为null,则具有一些后备逻辑.

When watching a directory, providing filename argument in the callback is not supported on every platform (currently it's only supported on Linux and Windows). Even on supported platforms filename is not always guaranteed to be provided. Therefore, don't assume that filename argument is always provided in the callback, and have some fallback logic if it is null.

这篇关于监听文件夹和文件(更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆