sweelix/yii2-plupload
Composer 安装命令:
composer require sweelix/yii2-plupload
包简介
PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads
README 文档
README
Sweelix Plupload extension for Yii 2 has been created to ease Plupload integration.
Plupload is not affiliated with Sweelix
Be carefull, Plupload (http://www.plupload.com/) is multi-licensed. Take care of the license which applies to :
- GPLv2 : http://www.plupload.com/license/gplv2
- Commercial : http://www.plupload.com/license/oem
Installation
If you use Packagist for installing packages, then you can update your composer.json like this :
{
"require": {
"sweelix/yii2-plupload": "*"
}
}
Howto use this extension
Once package has been installed:
activate Image management,by adding it to Yii components
// Yii2 app configuration components => [ // ... exiting components 'image' => [ 'class' => 'sweelix\yii2\image\Config', 'quality' => 80, 'cachingMode' => sweelix\image\Image::MODE_NORMAL, 'urlSeparator' => '/', 'cachePath' => '@webroot/cache', 'cacheUrl' => '@web/cache', 'errorImage' => 'error.jpg', ] // ... exiting components ]
add the extension in your Html helper class :
namespace app\components; use yii\helpers\Html as BaseHtml; use sweelix\yii2\plupload\traits\Plupload; class Html extends BaseHtml { // adding this trait allow easy access to plupload use Plupload; }
Basic usage
Sample one : one single file upload with basic UI and Automatic upload
The controller file
namespace app\controllers; use sweelix\yii2\plupload\components\UploadedFile; use yii\web\Controller; use Yii; /** * This is a basic controller */ class SiteController extends Controller { public function actions() { // add upload / preview and delete file management return [ 'async-upload' => 'sweelix\yii2\plupload\actions\UploadFile', 'async-delete' => 'sweelix\yii2\plupload\actions\DeleteFile', 'async-preview' => 'sweelix\yii2\plupload\actions\PreviewFile', ]; } public function actionIndex() { if(isset($_POST['demoUpload']) === true) { $uploads = UploadedFile::getInstancesByName('demoUpload'); // retrieve all uploaded files for name demoUpload foreach($uploads as $uploadedFile) { $uploadedFile->saveAs('@webroot/resources/'.$uploadedFile->name); } // ... perform correct redirection } $this->render('index'); } }
The index view file
// ... echo Html::asyncInput('demoUpload', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [ 'ui' => true, 'auto' => true, ]]); ?> //...
Sample two : multi-file upload with basic UI and Automatic upload
The controller file (nothing was changed in the controller)
namespace app\controllers; use sweelix\yii2\plupload\components\UploadedFile; use yii\web\Controller; use Yii; /** * This is a basic controller */ class SiteController extends Controller { public function actions() { // add upload / preview and delete file management return [ 'async-upload' => 'sweelix\yii2\plupload\actions\UploadFile', 'async-delete' => 'sweelix\yii2\plupload\actions\DeleteFile', 'async-preview' => 'sweelix\yii2\plupload\actions\PreviewFile', ]; } public function actionIndex() { if(isset($_POST['demoUpload']) === true) { $uploads = UploadedFile::getInstancesByName('demoUpload'); // retrieve all uploaded files for name demoUpload foreach($uploads as $uploadedFile) { $uploadedFile->saveAs('@webroot/resources/'.$uploadedFile->name); } // ... perform correct redirection } $this->render('index'); } }
The index view file, the square brackets here tell plupload to use multifile upload
// ... echo Html::asyncInput('demoUpload[]', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [ 'ui' => true, 'auto' => true, ]]); ?> //...
Config parameter this parameter allow the developper to configure plupload
Here are the default configuration
| PHP name | Plupload name | Default value |
|---|---|---|
| runtimes | runtimes | html5, html4 |
| multiSelection | multi_selection | false |
| maxFileSize | max_file_size | 0 |
| chunkSize | chunk_size | 10Mb |
| uniqueNames | unique_names | false |
| flashSwfUrl | flash_swf_url | null |
| silverlightXapUrl | silverlight_xap_url | null |
| browseButton | browse_button | null |
| dropElement | drop_element | null |
| container | container | null |
| multipart | multipart | null |
| multipartParams | multipart_params | null |
| requiredFeatures | required_features | null |
| filters | filters | null |
| headers | headers | null |
Model usage with manual file management
The model file
namespace app\models; use yii\db\ActiveRecord; use Yii; /** * Basic active record with uploadId (pkey autoincrement) and uploadFile (text) */ class Upload extends ActiveRecord { public static function tableName() { return '{{uploads}}'; } public function rules() { return [ // this rule is used to configure plupload : // * maxFiles trigger multifile upload, // * extensions trigger the plupload filters // * maxSize trigger the maxFileSize ['uploadFile', 'file', 'extensions' => ['jpg', 'png', 'm4a'], 'maxFiles' => 1, 'maxSize' => 450*1024], ]; } public function attributeLabels() { return [ 'uploadId' => Yii::t('sweelix', 'Upload ID'), 'uploadFile' => Yii::t('sweelix', 'Uploaded File'), ]; } }
The controller file
namespace app\controllers; use app\models\Upload; use sweelix\yii2\plupload\components\UploadedFile; use yii\web\Controller; use Yii; /** * This is a basic controller */ class SiteController extends Controller { public function actions() { // add upload / preview and delete file management return [ 'async-upload' => 'sweelix\yii2\plupload\actions\UploadFile', 'async-delete' => 'sweelix\yii2\plupload\actions\DeleteFile', 'async-preview' => 'sweelix\yii2\plupload\actions\PreviewFile', ]; } public function actionIndex() { $fileUpload = new Upload(); if($fileUpload->load($_POST) === true) { // ... perform pre save $uploads = UploadedFile::getInstances($fileUpload, 'uploadFile'); // retrieve all uploaded files for name demoUpload foreach($uploads as $uploadedFile) { // ... save file ... $uploadedFile->saveAs('@webroot/resources/'.$uploadedFile->name); } // ... perform post file save $fileUpload->save(); // ... perform correct redirection } $this->render('index', ['fileUpload' => $fileUpload]); } }
The index view file
// ... <?php echo Html::activeAsyncInput($fileUpload, 'uploadFile', ['config' => [ 'ui' => true, 'auto' => true, ]]); ?> //...
Model usage with automatic file management
The model file
namespace app\models; use sweelix\yii2\plupload\behaviors\AutomaticUpload; use yii\db\ActiveRecord; use Yii; /** * Basic active record with uploadId (pkey autoincrement) and uploadFile (text) */ class Upload extends ActiveRecord { public static function tableName() { return '{{uploads}}'; } public function behaviors() { return [ [ 'class' => AutomaticUpload::className(), 'attributes' => [ 'uploadFile' => [ // define where to save the file 'basePath' => '@webroot/resources', // define the url to access the file 'baseUrl' => '@web/resources', ], ] ] ]; } public function rules() { return [ // this rule is used to configure plupload : // * maxFiles trigger multifile upload, // * extensions trigger the plupload filters // * maxSize trigger the maxFileSize ['uploadFile', 'file', 'extensions' => ['jpg', 'png', 'm4a'], 'maxFiles' => 1, 'maxSize' => 450*1024], ]; } public function attributeLabels() { return [ 'uploadId' => Yii::t('sweelix', 'Upload ID'), 'uploadFile' => Yii::t('sweelix', 'Uploaded File'), ]; } }
The controller file
namespace app\controllers; use app\models\Upload; use sweelix\yii2\plupload\components\UploadedFile; use yii\web\Controller; use Yii; /** * This is a basic controller */ class SiteController extends Controller { public function actions() { // add upload / preview and delete file management return [ 'async-upload' => 'sweelix\yii2\plupload\actions\UploadFile', 'async-delete' => 'sweelix\yii2\plupload\actions\DeleteFile', 'async-preview' => 'sweelix\yii2\plupload\actions\PreviewFile', ]; } public function actionIndex() { $fileUpload = new Upload(); if($fileUpload->load($_POST) === true) { // ... file save is performed automagically $fileUpload->save(); // ... perform correct redirection } $this->render('index', ['fileUpload' => $fileUpload]); } }
The index view file
// ... <?php echo Html::activeAsyncInput($fileUpload, 'uploadFile', ['config' => [ 'ui' => true, 'auto' => true, ]]); ?> //...
Contributing
All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code.
Fork the project, create a feature branch , and send us a pull request.
sweelix/yii2-plupload 适用场景与选型建议
sweelix/yii2-plupload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.97k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2014 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「behaviors」 「plupload」 「asynchronous」 「yii」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sweelix/yii2-plupload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sweelix/yii2-plupload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sweelix/yii2-plupload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Doctrine2 behavior traits - slowhop fork
This TYPO3 CMS extension provides an API and a FE plugin for using plupload, a highly usable and advanced upload handler.
The plupload extension for the Yii framework
Doctrine2 behavior traits
Plupload component for Nette Framework.
统计信息
- 总下载量: 29.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-04-02