cohensive/upload
Composer 安装命令:
composer require cohensive/upload
包简介
Handle file uploading.
README 文档
README
Handle file uploading via standard HTML multipart form or via XHR data stream.
Usage
Upload can be used alone as any class or as a package for Laravel 4 framework (if you'd like to add support to any other frameworks, write to the issues or make a pull request with required changes).
First, add following line to the list of requirements in composer.json:
"cohensive/upload": "dev-master"
Run composer install or composer update to download it and autoload.
Laravel 4/5
Get package config file - not required, but maybe handy if you have a lot of various upload fields and you want to change default Upload options.
// Laravel 4 php artisan config:publish cohensive/upload // Laravel 5 php artisan vendor:publish
In providers array you need to add new package:
'providers' => array( //... 'Cohensive\Upload\UploadServiceProvider', //... )
In aliases add Upload facade:
'aliases' => array( //... 'Upload' => 'Cohensive\Upload\Facades\Laravel\Upload' //... )
Using in the code
// Create new Upload instance. // Both params are optional arrays. Description of allowed array items below. $upload = Upload::make($rules, $options); if ($upload->passes()) { // If file is valid, save it in the uploads (set in options) directory. $file = $upload->save(); // $file - is an instance of Cohensive\Upload\File class with various file-related attributes and methods. } else { // Get array of errors - simple list of failed validation rules. $upload->getErrors(); }
What to do with uploaded files next is out of the scope of Upload.
Standalone
As a standalone package you'll have to instantiate several classes that Upload depends on. Namely:
-
Validator - will validate files agains a set of rules.
-
Sanitizer - will keep filename safe for web (transliteration to ascii). It has one required parameter - instantiated class that will do the file sanitization. Various framework has own versions of such class often called Transliterator or Inflector. You just use its instance as a first argument and as a second parameter include method name that will be used for sanitization. Alternatively, you can create a wrapper for you Inflector which implements \Cohensive\Upload\Sanitizer\SanitizerInterface
-
FileHandlerFactory - will create FileHandlers based on the method of uploading
$validator = new \Cohensive\Upload\Validator([ 'maxSize' => 2097152 // Set maximum allowed filesize to 2MB (set in bytes) ]); $sanitizer = new \Cohensive\Upload\Sanitizer\AbstractSanitizer($sanitizerClass, $sanitizerMethod); $fileFactory = new \Cohensive\Upload\FileHandlerFactory(); $options = [...]; // An array of options for Upload, such as upload directory etc. $upload = new \Cohensive\Upload\Upload($validator, $sanitizer, $fileFactory, $options);
Using in the code
$rules = [...]; // An array of validation rules. Optional. if ($upload->passes($rules)) { // If file is valid, save it in the uploads (set in options) directory. $file = $upload->save(); } else { // Get array of errors - simple list of failed validation rules. $errors = $validator->getErrors(); }
Handling file post-save.
After file has been successfully validated and saved, Upload will return File
class instance which contains number of methods:
$file->move($destination); // Move file to another directory. Returns boolean. $file->delete(); // Delete file. $file->getFileinfo(); // Returns array containing file information. $file->getFilepath(); // Returns path to the file. $file->getMimetype(); // Returns file MIME. $file->getExtension(); // Returns file extension. $file->isExists(); // Checks if files exists. Returns boolean.
Options
Uploader accets an array of options on instantiation:
'uploadDir' => 'uploads/', // Folder where all uploaded files will be saved to. 'tmpDir' => 'uploads/tmp/', // Folder to keep files temporary for operations. 'param' => 'file', // Parameter to access the file on. 'name' => '', // Set new filename. Blank to use original name. 'nameLength' => 32, // Set maximum length of the name. Will be cut if longer. 'prefix' => '', // Add prefix to the filename.. 'suffix' => '', // Add suffix to the filename. 'case' => '', // Convert file name to the case: 'lower', 'upper' or falsy to keep original. 'overwrite' => false, // If file already exists, overwrite it. 'autoRename' => true, // In case if file with the same name exists append counter to the new file. 'randomize' => false, // Generate random filename. Boolean or integer for string length. Default length is 10. 'sanitize' => true // Sanitize filename - remove whitespaces and convert utf8 to ascii.
Rules
An array of rules that could be used while validation uploaded files:
'minSize' => 0, // Minimum filesize. 'maxSize' => 10485760, // Maximum filesize: 10MB. 'maxWidth' => 0, // Maximum image width if file is an image. 'maxHeight' => 0, // Maximum image height if file is an image. 'minWidth' => 0, // Minimum image width if file is an image. 'minHeight' => 0, // Minimum image height if file is an image. 'width' => [], // Image must have exact width (use array to set multiple). 'height' => [], // Image must have exact height (use array to set multiple). 'whiteExt' => ['jpg', 'jpeg', 'gif', 'png'], // Array of allowed extensions. 'blackExt' => [] // Array of disallowed extensions.
cohensive/upload 适用场景与选型建议
cohensive/upload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 729 次下载、GitHub Stars 达 2, 最近一次更新时间为 2014 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「HTML5」 「upload」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cohensive/upload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cohensive/upload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cohensive/upload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HTML5 support for SilverStripe
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.
The yii2-videojs-widget is a Yii 2 wrapper for the [video.js](http://www.videojs.com/). A JavaScript and CSS library that makes it easier to work with and build on HTML5 video. This is also known as an HTML5 Video Player.
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
统计信息
- 总下载量: 729
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-09-23