Download the PHP package webfiori/file without Composer
On this page you can find all versions of the php package webfiori/file. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webfiori/file
More information about webfiori/file
Files in webfiori/file
Informations about the package file
WebFiori File
A comprehensive PHP library for file operations, providing an object-oriented abstraction layer for reading, writing, uploading, and serving files with advanced features like Base64 encoding/decoding, MIME type detection, and chunked file processing.
Table of Contents
- Installation
- Requirements
- Supported PHP Versions
- Key Features
- Quick Start
- Core Classes
- Usage Examples
- Basic File Operations
- Reading Files
- Writing Files
- File Information
- File Upload
- Basic Upload
- Upload as File Objects
- Upload Configuration
- Base64 Encoding/Decoding
- File Serving
- Chunked Processing
- MIME Type Detection
- Working Example
- Testing
- API Reference
- File Class
- FileUploader Class
- UploadedFile Class
- MIME Class
- Error Handling
- Security Considerations
- Performance Tips
- Contributing
- License
Installation
Install via Composer:
Requirements
- PHP: 8.0 or higher
- Dependencies:
webfiori/jsonx
: ^4.0 (JSON serialization)webfiori/http
: * (HTTP response handling - dev dependency)
Supported PHP Versions
Build Status |
---|
Key Features
- Object-Oriented File Operations: Clean, intuitive API for file manipulation with comprehensive error handling
- Advanced File Upload: Multi-file upload with type validation, size limits, and detailed error reporting
- Base64 Encoding/Decoding: Built-in support for binary data encoding with strict validation options
- MIME Type Detection: Automatic MIME type detection for common file extensions
- File Serving: HTTP-compliant file serving with range request support for streaming
- Chunked Processing: Memory-efficient processing of large files with configurable chunk sizes
- JSON Serialization: Convert file objects to JSON for APIs and data interchange
- Path Normalization: Cross-platform path handling with automatic directory separator conversion
- Security Features: File type validation, directory traversal protection, and safe file operations
- Comprehensive Testing: Full test coverage with PHPUnit for reliability and stability
Quick Start
Core Classes
File
The main class for file operations, providing methods for reading, writing, and manipulating files.
FileUploader
Handles file uploads with validation, type checking, and error management.
UploadedFile
Extends File class to represent uploaded files with additional upload-specific properties.
MIME
Utility class for MIME type detection based on file extensions.
Usage Examples
Basic File Operations
Reading Files
Writing Files
File Information
File Upload
Basic Upload
Upload as File Objects
Upload Configuration
Base64 Encoding/Decoding
File Serving
Chunked Processing
MIME Type Detection
Working Example
The library includes a complete working example demonstrating file upload functionality with a modern web interface. The example consists of:
- Frontend: Vue.js application with Vuetify UI components
- Backend: PHP script using WebFiori File library
- Features: Drag-and-drop upload, progress feedback, error handling
Running the Example
The example demonstrates:
- File type validation (txt, doc, docx, png, jpg)
- Real-time upload progress
- Error handling and user feedback
- Secure file storage
For detailed setup instructions, see example/readme.md.
Testing
The library includes comprehensive test coverage using PHPUnit:
Test Coverage
The test suite covers:
- File Operations: Reading, writing, creating, deleting files
- Upload Functionality: Single and multi-file uploads, validation
- Base64 Encoding: Encoding/decoding with error handling
- MIME Detection: Extension-based MIME type detection
- Error Scenarios: Invalid paths, permissions, file not found
- Edge Cases: Empty files, large files, special characters
The library includes comprehensive test coverage with extensive test cases.
UploadedFile Class
Constructor
Core Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
isUploaded() |
Check if file was uploaded successfully | - | bool |
isReplace() |
Check if file replaced existing file | - | bool |
getUploadError() |
Get upload error message | - | string |
setIsUploaded($bool) |
Set upload status | bool $bool |
void |
setIsReplace($bool) |
Set replacement status | bool $bool |
void |
setUploadErr($err) |
Set upload error message | string $err |
void |
MIME Class
Static Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
getType($ext) |
Get MIME type by extension | string $ext |
string |
The MIME class contains a comprehensive mapping of file extensions to their corresponding MIME types.
Error Handling
The library uses the FileException
class for error handling:
Common exceptions thrown:
- File not found: When attempting to read non-existent files
- Permission denied: When lacking read/write permissions
- Invalid path: When providing malformed file paths
- Base64 decode error: When decoding invalid Base64 data
- Upload errors: Various upload-related failures
Security Considerations
File Upload Security
Path Traversal Protection
The library automatically:
- Normalizes directory separators across platforms
- Prevents directory traversal attacks
- Validates file paths before operations
- Uses secure file handling functions
Best Practices
- Always validate file types before processing
- Store uploads outside the web root when possible
- Set appropriate file permissions (644 for files, 755 for directories)
- Implement file size limits via PHP configuration
- Scan uploaded files for malware when possible
- Use HTTPS for file upload forms
Performance Tips
Memory Management
Optimization Tips
- Use appropriate chunk sizes for large files (1MB recommended)
- Read only necessary byte ranges when possible
- Cache MIME type detection results for repeated operations
- Use streaming for file serving when available
- Implement proper error handling to avoid resource leaks
File Serving Optimization
Contributing
We welcome contributions! Please follow these guidelines:
- Fork the repository and create a feature branch
- Write tests for new functionality
- Follow PSR-12 coding standards
- Update documentation for new features
- Submit a pull request with clear description
Development Setup
Running Tests
License
This project is licensed under the MIT License - see the LICENSE file for details.