webfiori / file
Basic class library to read, write and view files using PHP.
2.0.1
2026-04-26 00:09 UTC
Requires
- php: >=8.1
- webfiori/jsonx: 4.0.x
Requires (Dev)
- phpunit/phpunit: ^10.0
- webfiori/framework: dev-dev
- webfiori/http: *
README
A PHP library for file operations, providing an object-oriented abstraction layer for reading, writing, uploading, and serving files with Base64 encoding/decoding, MIME type detection, and chunked file processing.
Installation
composer require webfiori/file
Requirements
- PHP 8.1 or higher
webfiori/jsonx^4.0
Supported PHP Versions
| Build Status |
|---|
Quick Start
<?php require_once 'vendor/autoload.php'; use WebFiori\File\File; $file = new File('/path/to/document.txt'); $file->read(); echo $file->getRawData();
Core Classes
File— Read, write, create, remove, and serve files. Supports byte-range reads, Base64 encoding/decoding, chunked processing, and JSON serialization.FileUploader— Handle file uploads with extension validation, size limits, and detailed error reporting.UploadedFile— ExtendsFilewith upload-specific properties (upload status, replacement status, error message).MIME— Static lookup of ~600 file extension to MIME type mappings.
Examples
The examples/ directory contains runnable PHP scripts covering every feature of the library:
| Example | Description |
|---|---|
| Reading and Writing Files | Create, write, read, append, and remove files |
| File Information | File metadata: name, extension, MIME, size, timestamps, constructor variants |
| Partial Read | Read specific byte ranges from a file |
| Appending Data | Build up in-memory content with append() |
| Base64 Encoding | Encode/decode Base64, writeEncoded(), readDecoded() |
| Chunked Processing | Split file data into fixed-size chunks |
| Bytes and Hex | Convert data to byte arrays and hex strings |
| MIME Detection | Look up MIME types by extension |
| Error Handling | FileException scenarios and how to handle them |
| JSON Serialization | Convert File objects to JSON |
| Path Utilities | Path normalization, directory creation, file existence checks |
| File Upload | Configure and process uploads with FileUploader |
| Serving Files | Serve files over HTTP with proper headers |
Each example has its own README with detailed explanations. Run any example with:
php examples/read-and-write/read-and-write.php
Testing
# Run all tests composer test # Run tests with PHPUnit 10 composer test10
Contributing
- Fork the repository and create a feature branch
- Write tests for new functionality
- Follow PSR-12 coding standards
- Submit a pull request
License
MIT — see LICENSE for details.