rkwadriga / php-filereader
PHP file reader
dev-master
2020-09-09 06:48 UTC
Requires
- php: >=7.4.0
- phpunit/phpunit: ^9.3.8
This package is auto-updated.
Last update: 2025-06-09 17:02:05 UTC
README
REQUIREMENTS
The minimum requirement by this application template that your Web server supports PHP 7.4.0.
Install
$ composer require rkwadriga/php-filereader:dev-master
Supported formats: csv, sql, txt, log, json, yml, yaml
Usage
use rkwadriga\filereader\Factory; class MyApp { public function myFunction() { // Create a Factory instance $factory = new Factory(); // Create file reader (in this case .yml file reader) $fileReader = $factory->getReader('./config/main.yml'); // Read file (method "readFile" returns an associative array) $data = $fileReader->read(); // Write file $fileReader->write([ 'var1' => 'Value 1', 'var2' => 'Value 2', ]); } }
If you use the same dir for all files you work with, you can put this dir in Factory constructor and use relative files paths:
$factory = new Factory('./files_dir_path'); $fileReader = $factory->getReader('main.yml');
The file readers automatically crete file if it's not exist. If you don't want to do this, set second argument of "getReader" method to false:
$fileReader = $factory->getReader('main.yml', false);
In this case you will get the "File not found" exception trying to read not existed file.
If you have some specific file extension but that can be read like one of allowed extensions, you can set the "extensions map" of the readers factory:
$factory = new Factory(null, [ 'xjson' => 'json', 'xtxt' => 'txt', ... ]);