定制 initphp/upload 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

initphp/upload

Composer 安装命令:

composer require initphp/upload

包简介

Validate and store uploaded files on local disk, FTP/FTPS or Amazon S3 through a single adapter-based API.

README 文档

README

Validate and store uploaded files on local disk, FTP/FTPS or Amazon S3 through a single, adapter-based API.

CI Latest Stable Version Total Downloads License PHP Version Require

Requirements

  • PHP 8.0 or higher
  • ext-fileinfo (used to detect the real MIME type of a file)
  • ext-ftp — only for the FTP adapter
  • aws/aws-sdk-php — only for the S3 adapter

Installation

composer require initphp/upload

For S3 uploads, also install the AWS SDK:

composer require aws/aws-sdk-php

Quick start

require 'vendor/autoload.php';

use InitPHP\Upload\Upload;
use InitPHP\Upload\File;
use InitPHP\Upload\Adapters\LocalAdapter;

$adapter = new LocalAdapter([
    'dir' => __DIR__ . '/uploads/',
    'url' => 'https://example.com/uploads/',
], [
    'allowed_extensions' => ['jpg', 'jpeg', 'png', 'webp'],
    'allowed_max_size'   => 2 * 1024 * 1024, // 2 MB, in bytes
]);

$upload = new Upload($adapter);

foreach (File::setPost('photos') as $file) {
    $stored = $upload->setFile($file)->to();

    if ($stored !== false) {
        echo 'Uploaded to: ' . $stored->getURL();
    }
}

File::setPost('photos') reads $_FILES['photos'] and returns a normalized File[], whether the field uploaded one file or many. to() validates the file, stores it, and returns the stored File (with its public URL set) or false if the underlying write failed. Any validation or transfer problem is thrown as an UploadException.

Loading files

Call Returns Purpose
File::setPost(string $key) File[] Normalize $_FILES[$key] (single or multiple) into File objects.
File::setPath(string $path) File Wrap an existing file already on disk.

A file loaded with setPath() is copied to its destination (the source is left in place); a real HTTP upload is moved with move_uploaded_file().

Adapters

All three adapters share the same API — only the credentials differ. The optional second constructor argument is the validation options, identical across adapters.

Adapter Credentials Notes
LocalAdapter dir, url Creates missing destination directories.
FTPAdapter host, port, username, password, timeout, url, passive, ssl Binary mode, passive by default, optional FTPS.
S3Adapter key, secret_key, region, bucket, ACL, version Requires aws/aws-sdk-php.

The to($target) argument

$target means the same thing in every adapter: a destination path/key prefix. to('avatars/2026') stores the file under that sub-path:

  • Local → <dir>/avatars/2026/<name>
  • FTP → <remote>/avatars/2026/<name> (directories created as needed)
  • S3 → object key avatars/2026/<name> in the configured bucket

Call to() with no argument to store the file at the destination root.

Validation options

Pass these as the second constructor argument of any adapter (an empty array or 0 means "no restriction"):

Option Type Meaning
allowed_extensions string[] Allowed file extensions, matched case-insensitively against the original file name.
allowed_mime_types string[] Allowed MIME types, matched against the real type detected with finfo (not the client-supplied one).
allowed_max_size int Maximum size in bytes.

A file that violates any restriction — or whose upload errored — makes to() throw an UploadException.

Renaming

$file->rename('profile');          // keeps the original extension
$upload->setFile($file)->to();     // stored as profile.jpg

Documentation

Full guides with examples live in docs/:

Contributing

Bug reports and pull requests are welcome. CI runs PHP-CS-Fixer, PHPStan (max level) and PHPUnit across PHP 8.0–8.4; run the same bundle locally with:

composer ci

Credits

License

Copyright © 2023 InitPHP — released under the MIT License.

initphp/upload 适用场景与选型建议

initphp/upload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-19