herroffizier/yii2-upload-manager
Composer 安装命令:
composer require herroffizier/yii2-upload-manager
包简介
Simplifies file storing in Yii 2.
关键字:
README 文档
README
Yii2 Upload Manager is a small extension that organizes file uploads and takes control over upload paths and urls.
Features
- Groups uploads into folders and folder structures with any depth.
- Divides upload folders into subfolders to avoid storing many files in one folder.
- Fights against file name collisions.
- Uses transparent file name generation mechanism.
- 100% code coverage :-)
Installation
Install extension with Composer:
composer require herroffizier/yii2-upload-manager:@stable
Add extension to your application config:
'components' => [ // ... 'uploads' => [ 'class' => 'herroffizier\yii2um\UploadManager', // path to upload folder 'uploadDir' => '@webroot/upload', // url to upload filder 'uploadUrl' => '@web/upload', ], // ... ]
There is no need to create upload folder manually. Extension will make it automatically.
Usage
Storing files.
Extension provides few ways to store files.
Simply copy file to upload folder:
$filePath = Yii::$app->uploads->saveFile( // upload group 'useless-files', // upload file name 'file.txt', // original file name '/tmp/somefile.txt' );
Move file to upload folder:
$filePath = Yii::$app->uploads->moveFile( // upload group 'useless-files', // upload file name 'file.txt', // original file name '/tmp/somefile.txt' );
Save raw data as file in upload folder:
$content = 'test'; $filePath = Yii::$app->uploads->saveContent( // upload group 'useless-files', // upload file name 'file.txt', // file content $content );
Save \yii\web\UploadedFile instance to uplaod foder:
$upload = \yii\web\UploadedFile::getInstance(/* ... */); $filePath = Yii::$app->uploads->saveUpload( // upload group 'useless-files', // \yii\web\UploadedFile instance $upload );
As you may notice, all methods described above return $filePath value which is relative path to uploaded file and may be considered as unique upload id.
It can be converted to absolute file by method getAbsolutePath or to absolute url by method getUrl:
// get absolute path $absoluteFilePath = Yii::$app->uploads->getAbsolutePath($filePath); // get url $relativeUrl = Yii::$app->uploads->getUrl($filePath);
Name collisions
By deafult if you'll try to save file that already exists extension will throw an exception. Such behavior is not always suitable and you definitely don't want to solve each collision manually. So you have two different strategies which solve collisions automatically.
First strategy is to overwrite existing file silently. Such approach may be suitable for saving user avatars, for example.
Second strategy is to add incremental index to file name in case of collision. This strategy may be applied when dealing with user file uploads and original file names are important.
Now let's find out how to apply these strategies.
Strategy (throw exception, overwrite, add index) is identified by constant defined in \herroffizier\yii2um\UploadManager class: \herroffizier\yii2um\UploadManager::STRATEGY_KEEP, \herroffizier\yii2um\UploadManager::STRATEGY_OVERWRITE and \herroffizier\yii2um\UploadManager::STRATEGY_RENAME.
Also both methods saveContent and saveUpload have optional last parameter named $overwriteStrategy to which one of constants may be passed. Default value for $overwriteStrategy is \herroffizier\yii2um\UploadManager::STRATEGY_KEEP.
To sum up, let's take a look at example. Following code will throw an exception:
Yii::$app->uploads->saveContent( 'useless-files', 'file.txt', 'test 1' ); Yii::$app->uploads->saveContent( 'useless-files', 'file.txt', 'test 2' );
Hovewer, this code will work correctly because we applied overwrite strategy:
Yii::$app->uploads->saveContent( 'useless-files', 'file.txt', 'test 1' ); Yii::$app->uploads->saveContent( 'useless-files', 'file.txt', 'test 2', \herroffizier\yii2um\UploadManager::STRATEGY_OVERWRITE );
Now file.txt contains test 2 string.
Finally, let's apply rename strategy:
$filePath1 = Yii::$app->uploads->saveContent( 'useless-files', 'file.txt', 'test 1' ); $filePath2 = Yii::$app->uploads->saveContent( 'useless-files', 'file.txt', 'test 2', \herroffizier\yii2um\UploadManager::STRATEGY_RENAME ); echo "$filePath1, $filePath2";
This code will also complete correctly and output useless-files/75/file.txt, useless-files/75/file-1.txt. As you may see, second file has an index 1 at the end of its name.
herroffizier/yii2-upload-manager 适用场景与选型建议
herroffizier/yii2-upload-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.06k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「upload」 「extension」 「yii」 「yii2」 「yii 2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 herroffizier/yii2-upload-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 herroffizier/yii2-upload-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 herroffizier/yii2-upload-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 2.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2015-10-26