PHP code example of brunonatali / file

1. Go to this page and download the library: Download brunonatali/file library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brunonatali / file example snippets


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 is automatically Change;

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
    */
}

$config = [
    'client_name' => 'FILE-X', // Desired app name when P 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 
];

/**
 * 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);

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

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

$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');
}

$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);