yohns / filewriter
File writing utility for PHP
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/yohns/filewriter
Requires
- php: ^7.4|^8.0
 
Requires (Dev)
- phpunit/phpunit: ^11.4
 
README
Overview
The FileWriter class provides a simple and robust utility for writing content to files with various operations like overwriting, appending, and prepending.
Installation
Install via Composer:
composer require yohns/filewriter
Requirements
- PHP 7.4+
 - Ext-fileinfo recommended
 
Usage Examples
Basic File Writing
use Yohns\Util\FileWriter; // Create a new FileWriter instance $fileWriter = new FileWriter('path/to/your/file.txt'); // Overwrite file content $fileWriter->overwrite("Hello, world!\n"); // Append to file $fileWriter->append("Additional content\n"); // Prepend to file $fileWriter->prepend("Initial content\n");
Handling File Operations
$fileWriter = new FileWriter('logs/app.log'); // Safely write content, with error handling if (!$fileWriter->append("Log entry: " . date('Y-m-d H:i:s') . "\n")) { // Handle writing error error_log("Could not write to log file"); }
Features
- Automatic directory creation
 - File creation if not exists
 - Overwrite, append, and prepend operations
 - File locking to prevent race conditions
 
Error Handling
Methods return boolean values:
true: Operation successfulfalse: Operation failed
License
MIT License