arraypress/wp-file-utils
Composer 安装命令:
composer require arraypress/wp-file-utils
包简介
A comprehensive WordPress library for file operations, path manipulation, MIME type handling, and secure filesystem operations.
README 文档
README
A lightweight WordPress library providing essential file path operations, MIME type detection, and security validation.
Installation
composer require arraypress/wp-file-utils
Requirements
- PHP 7.4 or later
- WordPress 5.0 or later
Core Classes
File Class
Path/URL conversions and path manipulation that WordPress doesn't provide natively.
use ArrayPress\FileUtils\File; // Convert between URLs and paths (the real value of this library) $path = File::url_to_path( 'https://site.com/wp-content/uploads/file.pdf' ); $url = File::path_to_url( '/var/www/wp-content/uploads/file.pdf' ); // Resolve paths within the uploads directory $path = File::upload_path( 'sugarcart/2024/01/file.zip' ); $url = File::upload_url( 'sugarcart/2024/01/file.zip' ); // Check if file is local if ( File::is_local_file( $url ) ) { // Safe to convert to local path } // Change extension $new_path = File::change_extension( 'image.jpg', 'png' ); // 'image.png' // Rename with preserved extension $filename = File::rename_preserve_extension( 'My Report', 'original.pdf' ); // 'my-report.pdf' // Join paths safely $full_path = File::join_path( $upload_dir, '2024', 'documents', 'file.pdf' ); // Normalize cross-platform paths $clean = File::normalize_path( 'path\\to//file.pdf' ); // 'path/to/file.pdf'
MIME Class
MIME type detection and categorization with 70+ file type mappings.
use ArrayPress\FileUtils\MIME; // Get MIME type from filename $mime = MIME::get_type( 'document.pdf' ); // 'application/pdf' // Get extension from MIME type $ext = MIME::get_extension_from_type( 'application/pdf' ); // 'pdf' // Determine extension from HTTP response (useful for sideloading) $ext = MIME::get_extension_from_response( $response, $body ); // 'webp' // Determine delivery behavior if ( MIME::should_force_download( $mime ) ) { // Force download (ZIP, DOC, executables, etc.) } else { // Display inline (PDF, images, video, audio) } // Type checking MIME::is_media( $mime ); // Audio or video MIME::is_image( $mime ); // Image files MIME::is_document( $mime ); // Office docs, PDFs, text MIME::is_archive( $mime ); // ZIP, RAR, 7Z, etc. // Get all registered types $all = MIME::get_all_types(); // ['pdf' => 'application/pdf', ...]
Security Class
File security validation and path sanitization.
use ArrayPress\FileUtils\Security; // Check if filename is safe (no traversal, null bytes, or dangerous extensions) if ( Security::is_safe_filename( $filename ) ) { // Safe to use } // Check against an allowlist $allowed = [ 'pdf', 'doc', 'docx', 'jpg', 'png' ]; if ( Security::is_allowed_file_type( 'document.pdf', $allowed ) ) { // File type is permitted } // Sanitize user-supplied paths (strips phar://, php://, traversal, etc.) $safe_path = Security::sanitize_path( $user_input );
Supported MIME Types
The library includes mappings for 70+ file types:
- Documents: PDF, Word, Excel, PowerPoint, OpenDocument
- Media: MP3, MP4, WebM, AVI, MOV, OGG, WAV, FLAC
- Images: JPEG, PNG, GIF, WebP, SVG, BMP, TIFF
- Archives: ZIP, RAR, 7Z, TAR, GZ, BZ2
- E-books: EPUB, MOBI, AZW
- Text: TXT, CSV, JSON, XML, HTML, CSS, JS, RTF
- Fonts: TTF, OTF, WOFF, WOFF2
- Applications: EXE, DMG, APK, DEB, RPM
Why This Library?
WordPress lacks several essential file operations:
- URL ↔ path conversion — no built-in way to convert between file URLs and local paths
- Upload path resolution — no simple helper to resolve paths within the uploads directory
- Smart delivery detection — no logic for determining inline vs download behavior
- Comprehensive MIME mappings — limited file type coverage beyond basics
- MIME from HTTP responses — no detection from response headers or binary inspection
- Path security — no protection against protocol injection (
phar://,php://, etc.)
Integration Examples
File Upload Validation
use ArrayPress\FileUtils\Security; use ArrayPress\FileUtils\MIME; function validate_upload( string $filename ): bool|WP_Error { if ( ! Security::is_safe_filename( $filename ) ) { return new WP_Error( 'unsafe', 'Filename contains unsafe characters.' ); } $allowed = [ 'pdf', 'doc', 'docx', 'jpg', 'png' ]; if ( ! Security::is_allowed_file_type( $filename, $allowed ) ) { return new WP_Error( 'invalid_type', 'File type not allowed.' ); } return true; }
Sideloading Remote Images
use ArrayPress\FileUtils\MIME; $response = wp_remote_get( $image_url ); $body = wp_remote_retrieve_body( $response ); // Detect extension even when URL has no file extension $ext = MIME::get_extension_from_response( $response, $body ); $filename = 'product-image.' . $ext;
Download Handling
use ArrayPress\FileUtils\File; use ArrayPress\FileUtils\MIME; $local_path = File::url_to_path( $download_url ); if ( $local_path && is_readable( $local_path ) ) { $mime = MIME::get_type( $local_path ); if ( MIME::should_force_download( $mime ) ) { // Serve as download } else { // Display inline } }
License
GPL-2.0-or-later
Credits
Created and maintained by David Sherlock at ArrayPress.
arraypress/wp-file-utils 适用场景与选型建议
arraypress/wp-file-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「path」 「filesystem」 「security」 「wordpress」 「mime」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/wp-file-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/wp-file-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/wp-file-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
The flysystem adapter for yandex disk rest api.
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2025-09-02