Search code examples
phpfilenode.jsdirectorylisten

Listening folders and files (changes)


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?

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").

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.

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


Solution

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

    Simple example:

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

    Note:

    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.