https://github.com/yohn/filewriter
Simple PHP file writing class
https://github.com/yohn/filewriter
Last synced: 4 months ago
JSON representation
Simple PHP file writing class
- Host: GitHub
- URL: https://github.com/yohn/filewriter
- Owner: Yohn
- License: mit
- Created: 2024-11-22T20:56:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-22T22:21:05.000Z (over 1 year ago)
- Last Synced: 2025-03-28T00:04:27.055Z (about 1 year ago)
- Language: PHP
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FileWriter PHP Utility
## 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:
```bash
composer require yohns/filewriter
```
## Requirements
- PHP 7.4+
- Ext-fileinfo recommended
## Usage Examples
### Basic File Writing
```php
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
```php
$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 successful
- `false`: Operation failed
## License
MIT License