webfiori/file 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webfiori/file

Composer 安装命令:

composer require webfiori/file

包简介

Basic class library to read, write and view files using PHP.

README 文档

README

A PHP library for file operations, providing an object-oriented abstraction layer for reading, writing, uploading, and serving files with streaming support, Base64 encoding/decoding, MIME type detection, and chunked file processing.

PHP 8.1+

Table of Contents

Supported PHP Versions

Build Status

Key Features

  • Object-oriented file read, write, create, remove, copy, and move
  • Streaming I/O with constant memory usage via FileStream
  • File upload handling with extension validation, size limits, and callback hooks
  • Stream processing during uploads with hash verification support
  • Atomic writes (temp + rename) for crash-safe operations
  • Base64 encoding/decoding
  • Byte-range reads for partial file access
  • MIME type detection for ~600 file extensions
  • ResponseEmitter interface for framework-agnostic HTTP file serving
  • FileInterface for dependency injection and mocking

Why This Library?

  • All-in-one — File I/O, uploads, streaming, and HTTP serving in a single package.
  • Upload-first designFileUploader, StreamingUploader, and ResumableUploader handle multipart forms, raw body streams, and chunked uploads with pause/resume — each with extension filtering, size limits, and callback hooks.
  • Constant-memory streaming — Generators power reads, writes, uploads, and serving. Process gigabyte files without touching memory limits.
  • Framework-agnostic HTTP serving — The ResponseEmitter interface decouples file serving from any specific HTTP layer. Plug in PSR-7, any framework, or raw PHP.
  • Testable by designFileInterface enables dependency injection and mocking. No filesystem required in your unit tests.
  • Lightweight — Single dependency, no framework coupling.

Installation

composer require webfiori/file

Requirements:

  • PHP 8.1 or higher
  • webfiori/jsonx ^4.0

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

  • FileInterface — Contract defining core file operations. Use for type-hinting and mocking.
  • File — Read, write, create, remove, copy, move, and serve files. Supports byte-range reads, Base64 encoding/decoding, chunked processing, and JSON serialization.
  • FileStream — Streaming file I/O with constant memory usage. Read chunks, lines, ranges, and serve large files.
  • FileUploader — Handle multipart form file uploads with extension validation, size limits, stream processing, and callback hooks.
  • StreamingUploader — Receive files from raw HTTP body (php://input) in constant memory. Ideal for single-shot binary uploads.
  • ResumableUploader — Chunked upload handler with resume-on-failure support. Tracks byte offset on disk for seamless resume after network drops.
  • UploadedFile — Extends File with upload-specific properties (upload status, replacement status, error message).
  • ResponseEmitter — Interface for abstracting HTTP output when serving files.
  • MIME — Static lookup of ~600 file extension to MIME type mappings.

Using FileInterface

Type-hint FileInterface when your code only needs I/O operations. Use the concrete File class when you need encoding, serialization, or HTTP features.

use WebFiori\File\FileInterface;

class DocumentService {
    public function process(FileInterface $file): string {
        $file->read();
        return strtoupper($file->getRawData());
    }
}

Mocking in Tests

use WebFiori\File\FileInterface;

$mockFile = $this->createMock(FileInterface::class);
$mockFile->method('getRawData')->willReturn('test content');
$mockFile->method('getName')->willReturn('test.txt');
$mockFile->method('isExist')->willReturn(true);

$service = new DocumentService();
$result = $service->process($mockFile);

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 ResponseEmitter
Streaming I/O Read chunks, lines, and ranges with FileStream
Copy and Move Copy and move files with streaming
Atomic Write Crash-safe writes with temp + rename
Streaming Upload Receive large files from php://input
Upload Callbacks Before/after hooks for validation and logging
Resumable Upload Chunked upload with pause/resume support
Custom FileInterface Implement FileInterface for DI, mocking, or custom storage
Custom Emitter Implement ResponseEmitter for framework integration

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.

Support

Found a bug or have a feature request? Open an issue.

Changelog

See CHANGELOG.md for release history.

webfiori/file 适用场景与选型建议

webfiori/file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.95k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「file」 「upload」 「files」 「uploading」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 webfiori/file 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webfiori/file 我们能提供哪些服务?
定制开发 / 二次开发

基于 webfiori/file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 28.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 20
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-10