定制 samsonphp/upload 二次开发

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

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

samsonphp/upload

最新稳定版本:1.1.2

Composer 安装命令:

composer require samsonphp/upload

包简介

README 文档

README

Latest Stable Version Build Status Code Coverage Total Downloads Scrutinizer Code Quality Stories in Ready

Connection

To use this module in your project you must add dependency in your composer.json:

"require": {
    "samsonphp/upload": "1.*"
},

After doing composer install or composer update you will get all classes and functions of Upload module.

Configuration

Configuration for Upload module is not necessary, but you can use it if you want to create unique file name or upload dir name for uploading files.
If you will not use configuration, your upload catalog will be called 'upload' and file name will be random generated.

Thanks to SamsonPHP module/service configuration you can create simple config class:

class UploadConfig extends \samson\core\Config
{
    // Callback function for creating upload dir
    public $uploadDirHandler = 'uploadDirHandler';

    // Callback function for creating file name
    public $fileNameHandler = 'uploadNameHandler';
}

Create async file uploading

The main feature of current module is asynchronous file uploading. Look at the example.

Example of HTML code

First of all you need to create html container with your file input:

<p>
    <input type="hidden" class="__action" value="<?php url_base('catalog/upload'); ?>">
    <input type="hidden" class="__file_size" value="50000000">
    <input class="__example_upload" type="file" name="uploadFile">
</p>
  • Input with class "__action" defines handler of your uploading. It can be controller method in your bundle or simple controller function. It always must have class "__action".
  • Input with class "__file_size" defines max size of uploading file. It always must have class "__file_size".
  • Input with class "__example_upload" is input button for choosing file from clients computer. If you are using class name "__upload" for this input, you don't need to create some javascript code in order for everything to work.

Also you can add following feature blocks :

  • Container with class "__progress_text" will show you percents of file uploading.
  • Paragraph tag inside container with class "__progress_bar" will create animation as progress bar of uploading.

Remember, that you must add some css styles for this blocks to use them as you want.

Example of javascript code

If you are using input with class different from "__upload", you need to handle you inputs:

// Cache file input
var file = s('.__example_upload');

// Bind upload event
uploadFileHandler(file, {
    // Handle event before file uploading
    start: function () {
        alert('Uploading started');
    },
    // Handle event after upload finishing
    response: function (response) {
        alert('Uploading finished');
    }
});

The second parameter in function uploadFileHandler(file, options) is not necessary, you can use it for creating something special while file is uploading.

Example of uploading handler

Server action in our HTML example is controller 'catalog/upload'. Let's create this function:

function catalog_async_upload()
{
    // Parameter for callback functions
    $parameter = 5;

    // Create AJAX response array
    $json = array('status' => 0);

    // Create object for uploading file to server
    $upload = new \samsonphp\upload\Upload(array('png','jpg'), $parameter);

    if ($upload->upload($filePath, $fileName, $realName) {
        $json['status'] = 1;
        // Add full path to uploaded file into AJAX response
        $json['filePath'] = $filePath;
        // Add uploaded file name into AJAX response
        $json['fileName'] = $fileName;
        // Add real file name (user's file name) into AJAX response
        $json['realName'] = $realName;
    }

    return $json;
}

To create file upload you need to create class \samsonphp\upload\Upload, constructor of which can have three parameters.

  • First parameter is array of allowable file extensions for uploading file.
  • Second is array of parameters (or just one parameter) for your callback functions. It can be used if you are using module configuration.
  • The third one is configuration class. This is system parameter which default value is m('upload'). Better do not specify it, if you are working with simple upload module.

Method that directly create uploading called upload(& $filePath = '', & $fileName = '', & $realName = ''). You can get main file information using parameters of this method.

Also we can create our callback functions that are defined in configuration class:

/**
* External handler for defining upload catalog
*/
function uploadDirHandler($parameter)
{
    return 'upload/catalog'.$parameter;
}

/**
* External handler for defining upload file name
*/
function uploadNameHandler($parameter, $extension)
{
    return 'item'.$parameter.$extension;
}

As you see, we have parameter $extension in our name handler function. This parameter is always defined as last parameter of this callback function.
In our case after created callback functions, upload catalog will be called 'upload/catalog5' and file name will be like 'item5'.
Don't forget, that in current example all uploading files will have simple name (maybe different extensions), so you must come up with some logic for using upload file name handler correctly.

Developed by SamsonOS.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 5
  • 开发语言: JavaScript

其他信息

  • 授权协议: Open
  • 更新时间: 2015-02-10