khanzadimahdi/uploadmanager 问题修复 & 功能扩展

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

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

khanzadimahdi/uploadmanager

Composer 安装命令:

composer require khanzadimahdi/uploadmanager

包简介

simple upload manager

README 文档

README

screen record

Description

File upload manager can be used to upload chunk and non-chunk files.

Uploads can be resumed later(see "resumable-chunk-upload" example in "examples/js-example directory").

Add your files , upload them and close browser, next time you can open browser and resume the uncompleted uploads.

Features

  • Multiple file upload: Allows to select multiple files at once and upload them simultaneously.
  • Cancelable uploads: Individual file uploads can be canceled to stop the upload progress.
  • Resumable uploads: Aborted uploads can be resumed later.
  • Chunk uploads: Large files can be uploaded in smaller chunks.
  • Customizable and extensible: Provides an interface to define callback methods for various upload events.

Getting started

Installation

composer require khanzadimahdi/uploadmanager

Usage

Available Classes :

  • UploadManager\Chunk : contains file's (or chunk) information.
  • UploadManager\Upload : stores received files (or chunks).

Example: (simple file upload)

Files are in the "examples/simple-upload" directory

First we create a simple HTML form

<form method="post" action="<?= htmlentities($_SERVER['PHP_SELF']) ?>" enctype="multipart/form-data">
    <label for="media">select file to upload:</label>
    <input id="media" name="media" type="file">
    <input type="submit" value="upload files">
</form>

Then we store file (or files) using the below codes:

if($_SERVER['REQUEST_METHOD']=='POST'){
    $uploadManager=new \UploadManager\Upload('media');
    $chunks=$uploadManager->upload('uploads');
    if(!empty($chunks)){
        foreach($chunks as $chunk){
            echo '<p class="success">'.$chunk->getNameWithExtension().' has been uploaded successfully</p>';
        }
    }
}

if uploaded file has any errors or it cant be uploaded , the "upload" method throws an exception, so it's better to call this method in a try catch block.

try{
    if($_SERVER['REQUEST_METHOD']=='POST'){
        $uploadManager=new \UploadManager\Upload('media');
        $chunks=$uploadManager->upload('uploads');
        if(!empty($chunks)){
            foreach($chunks as $chunk){
                echo '<p class="success">'.$chunk->getNameWithExtension().' has been uploaded successfully</p>';
            }
        }
    }
}catch(\UploadManager\Exceptions\Upload $exception){
    //if file exists: (user selects a file)
    if(!empty($exception->getChunk())){
        foreach($exception->getChunk()->getErrors() as $error){
            echo '<p class="error">'.$error.'</p>';
        }
    }else{
        echo '<p class="error">'.$exception->getMessage().'</p>';
    }
}

Each file (or chunk) related errors can be retrieved by "getErrors" method as an array.

Validations

We can validate files (or chunks) before storing them.

a simple validation example is in "examples/using-validations" directory

Available validations :

  1. Extension
  2. MimeType
  3. Size

Example:

//add validations
$uploadManager->addValidations([
    new \UploadManager\Validations\Size('2M'), //maximum file size is 2M
    new \UploadManager\Validations\Extension(['jpg','jpeg','png','gif']),
]);

as you see, we can use "addValidations" method to add an array of validations.

you can use either available validations or creating a custom validation by implementing "UploadManager\Contracts\ValidationInterface" interface.

Callbacks

Here we have the below callbacks:

  1. beforeValidate

  2. afterValidate

  3. beforeUpload

  4. afterUpload

Callbacks can be used to control the flow of uploading mechanism.

example:

Here we remove uploaded file (or chunk) if an error occurred.

Files are in the "examples/simple-callbacks" directory

//add callback : remove uploaded chunks on error
$uploadManager->afterValidate(function($chunk){
    $address=($chunk->getSavePath().$chunk->getNameWithExtension());
    if($chunk->hasError() && file_exists($address)){
        //remove current chunk on error
        @unlink($address);
    }
});

More real examples

see the "examples/js-examples" directory for more real examples.

available examples:

  • Dropzone

  • JQuery file upload

  • Plupload

  • Resumable-Chunk-Upload : the best example for resuming uploads in the future! in this example you need to create a database and import "server/medias.sql" in it , then change connection configs in "server/upload.php"

Requirements

requirements

Optional requirements

  • Json ext : Used to return json messages can be used in client side.

Contributing

Please read the contribution guidelines before submitting a pull request.

Support

This project is actively maintained, but there is no official support channel.

License

Released under the MIT license.

khanzadimahdi/uploadmanager 适用场景与选型建议

khanzadimahdi/uploadmanager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.05k 次下载、GitHub Stars 达 69, 最近一次更新时间为 2018 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 23.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 69
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 69
  • Watchers: 6
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-08-30