PHPackages                             brunonatali/file - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. brunonatali/file

ActiveLibrary[File &amp; Storage](/categories/file-storage)

brunonatali/file
================

A handful file tools

v0.1.1(5y ago)029MITPHPPHP &gt;=7

Since Jan 24Pushed 5y ago1 watchersCompare

[ Source](https://github.com/brunonatali/file)[ Packagist](https://packagist.org/packages/brunonatali/file)[ RSS](/packages/brunonatali-file/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

File
====

[](#file)

Use this library to manipulate and interact with the file system.
Note. Some functions or classes cannot interact with the Windows system, so use it with caution.

**WARNING**: The [OnFileChange](#on-file-change) class needs [reactphp/event-loop](https://github.com/reactphp/event-loop) to work, but it was not added as required in the composer configuration so as not to force the average user to include a library that they will never use, so don't forget to require it when installing your program, if you are going to use this class.

**Table of Contents**

- [OnFileChange](#onfilechange)
    - [Polling example](#polling-example)
    - [Inotify example](#inotify-example)
    - [OFC Configuration](#ofc-configuration)
    - [OFC start()](#ofc-start)
    - [OFC stop()](#ofc-stop)
    - [OFC setPollingTime()](#ofc-setpollingtime)
    - [OFC static isFileChanged()](#ofc-static-isfilechanged)
- [JsonFile](#jsonfile)
    - [readAsArray()](#readasarray)
    - [saveArray()](#savearray)
- [FileHandler](#filehandler)
    - [Not documented yet](#fh-not-doc)
- [Install](#install)
- [License](#license)

OnFileChange
------------

[](#onfilechange)

OnFileChange is a little help for you to monitor when a file has been modified and take some action from it.

**Read the following notes**

This class supports debug by including [brunonatali/tools](https://github.com/brunonatali/tools) in your composer project.

**ATENTION**: The [OnFileChange](#on-file-change) class needs [reactphp/event-loop](https://github.com/reactphp/event-loop) to work, include it by hand in your project!

**PERFORMANCE**: For performance purpouses install the [inotify](https://pecl.php.net/package/inotify) PECL extension and include [brunonatali/inotify](https://github.com/brunonatali/inotify) in your project.

### Polling example

[](#polling-example)

```
use BrunoNatali\File\OnFileChange;

use React\EventLoop\Factory as LoopFactory;

$loop = LoopFactory::create();

$myFuncToCall = function () {
    echo "File changed!";
};

try {
    $onFileChange = new OnFileChange(
        '/my/path/to.file',
        $loop,
        $myFuncToCall,
        /**
         * You can pass an configuration array to force polling or configure polling time
         * but generally system will use polling if brunonatali/inotify was not found and
         * a 1 sec polling time as default
        */
        [
            'force_ppolling' => true,
            'polling_time' => 1.0 // check for file changes every 1 sec
        ]
    );
} catch ($e \Exception) {
    /**
     * Exception codes:
     * ERROR_FILE_NAME_ABSENT -> file name not provided
     * ERROR_FILE_CALL_ABSENT -> no callable function
     * ERROR_FILE_LOOP_ABSENT -> unknown LoopInterface
     * ERROR_FILE_NOT_EXIST -> non existent file
    */
}
```

### Inotify example

[](#inotify-example)

Install [brunonatali/inotify](https://github.com/brunonatali/inotify) in your project by typing:

```
composer require brunonatali/inotify
```

```
// Inotify is automatically included if available
use BrunoNatali\File\OnFileChange;

use React\EventLoop\Factory as LoopFactory;

$loop = LoopFactory::create();

$myFuncToCall = function () {
    echo "File changed!";
};

try {
    $onFileChange = new OnFileChange('/my/path/to.file', $loop, $myFuncToCall);
} catch ($e \Exception) {
    /**
     * Exception codes:
     * ERROR_FILE_NAME_ABSENT -> file name not provided
     * ERROR_FILE_CALL_ABSENT -> no callable function
     * ERROR_FILE_LOOP_ABSENT -> unknown LoopInterface
     * ERROR_FILE_NOT_EXIST -> non existent file
    */
}
```

### OFC Configuration

[](#ofc-configuration)

Configuations are passed in array format on 4th OnFileChange() arg as follows:

```
$config = [
    'client_name' => 'FILE-X', // Desired app name when included and using debug mode []
    'auto_start' => true, // Tell if file monitoring will start ASAP as initialized. If false, start() is necessary
    'force_ppolling' => false, // Force polling mothod instead inotify
    'polling_time' => 1.0, // Check file change period when rining on polling method
    /**
     * Provide an Inotify constant to filter specific attr change
     *   NOTE 1. To specify more than one constant place each one in array (see below).
     *   NOTE 2. 'specific_attr' has no effect when running on polling method
    */
    'specific_attr' => false // Pasing a single flag as simple like this => IN_MODIFY
];

// https://www.php.net/manual/en/inotify.constants.php
$inotifyFilters = [
    IN_ACCESS,
    IN_MODIFY
];
```

### OFC start()

[](#ofc-start)

Used to starts watching file change. Only takes effect when stoped or initialized with 'auto\_start' =&gt; false.
This function dos not return nothing.

### OFC stop()

[](#ofc-stop)

Stops file change verification. This function dos not return nothing.

### OFC setPollingTime()

[](#ofc-setpollingtime)

Configure polling time when using this method.

```
/**
 * Set file verification for every 10 sec.
 * Returns FALSE if is not configured to use polling method or can`t stops running timer
*/
$onFileChange->setPollingTime(10.0);
```

### OFC static isFileChanged()

[](#ofc-static-isfilechanged)

You can manually check file changes by calling isFileChanged(). This function is provided statically to could be called by hand

```
$file = '/my/path/to.file';
$lastModifiedDate = null;

while (true) {
    if (OnFileChange::isFileChanged($file, $lastModifiedDate))
        echo "File checked manually & was changed!";
    sleep(5);
}
```

JsonFile
--------

[](#jsonfile)

This class is available with static functions for easy interaction, with the objective of easy manipulation / creation of JSON files.

### readAsArray()

[](#readasarray)

Reads entire json file as array. This function is meant to be a function to simplify use of native PHP functions file\_get\_contents() and json\_decode(), adding some validations.
Simple call:

```
$jsonArray = \BrunoNatali\File\JsonFile\readAsArray('\my\path\to\file.json');

/**
 * You can add paths to search desired file.
 * Than file will be searched in every provided paths and than readed
*/
$anotherJsonArray = \BrunoNatali\File\JsonFile\readAsArray('file.json', '\my\path\one', '\my\path\two');
}
```

### saveArray()

[](#savearray)

Save provided array to a JSON file, returning a boolean success result.
You can provide [PHP JSON flags](https://www.php.net/manual/en/json.constants.php).

```
$dataArray = [
    'simple-obj' => [
        1,2,3,4,5
    ]
];
/**
 * Comom use
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('\my\path\to\file.json', $dataArray);

/**
 * Overwrite destiny if exists
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('\my\path\to\file.json', $dataArray, true);

/**
 * Adding paths to search desired write file
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('file.json', $dataArray, true, '\my\path\one', '\my\path\two');

/**
 * Adding flags to PHP JSON function
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('\my\path\to\file.json', $dataArray, JSON_PRETTY_PRINT | JSON_HEX_TAG);
```

FileHandler
-----------

[](#filehandler)

File handler was designed to be an stream file reader, but was not reviewed and not documented yet.

Install
-------

[](#install)

The recommended way to install this library is [through Composer](https://getcomposer.org). [New to Composer?](https://getcomposer.org/doc/00-intro.md)

This project follows [SemVer](https://semver.org/). This will install the latest supported version:

```
$ composer require brunonatali/file:^0.1
```

This project aims to run on Linux and require other components and [inotify PHP extension](https://pecl.php.net/package/inotify), to work properly, follow each section instructions to get what you need.
If you find a bug, please report.

License
-------

[](#license)

MIT, see [LICENSE file](LICENSE).

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Total

2

Last Release

1913d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d7cd5291428261c565ed40d405cf12f06166ce3fcf3f5d8d0f292d3d283cd8c5?d=identicon)[brunonatali](/maintainers/brunonatali)

---

Top Contributors

[![brunonatali](https://avatars.githubusercontent.com/u/28903793?v=4)](https://github.com/brunonatali "brunonatali (7 commits)")

---

Tags

filefolder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brunonatali-file/health.svg)

```
[![Health](https://phpackages.com/badges/brunonatali-file/health.svg)](https://phpackages.com/packages/brunonatali-file)
```

###  Alternatives

[league/flysystem

File storage abstraction for PHP

13.6k639.1M2.2k](/packages/league-flysystem)[league/flysystem-aws-s3-v3

AWS S3 filesystem adapter for Flysystem.

1.6k263.6M790](/packages/league-flysystem-aws-s3-v3)[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[knplabs/knp-gaufrette-bundle

Allows to easily use the Gaufrette library in a Symfony project

72528.6M91](/packages/knplabs-knp-gaufrette-bundle)[league/flysystem-local

Local filesystem adapter for Flysystem.

226231.8M39](/packages/league-flysystem-local)[league/flysystem-memory

In-memory filesystem adapter for Flysystem.

8533.6M194](/packages/league-flysystem-memory)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
