Skip to content

WebFiori/file

WebFiori File

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 — Extends File with 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

  1. Fork the repository and create a feature branch
  2. Write tests for new functionality
  3. Follow PSR-12 coding standards
  4. Submit a pull request

License

MIT — see LICENSE for details.

About

Class library to read, write and, view files in PHP.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

 
 
 

Contributors

Languages