2017 © Pedro Peláez
 

yii2-extension yii2-plupload

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

image

sweelix/yii2-plupload

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  • Thursday, November 26, 2015
  • by pgaultier
  • Repository
  • 3 Watchers
  • 16 Stars
  • 3,211 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 4 Open issues
  • 6 Versions
  • 4 % Grown

The README.md

Sweelix Yii2 Plupload extension

Sweelix Plupload extension for Yii 2 has been created to ease Plupload integration., (*1)

Plupload is not affiliated with Sweelix, (*2)

Be carefull, Plupload (http://www.plupload.com/) is multi-licensed. Take care of the license which applies to :, (*3)

  • 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 :, (*4)

``` json { "require": { "sweelix/yii2-plupload": "*" } }, (*5)


Howto use this extension ------------------------ Once package has been installed: activate Image management,by adding it to Yii components ``` php // 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 :, (*6)

``` php, (*7)

namespace app\components;, (*8)

use yii\helpers\Html as BaseHtml; use sweelix\yii2\plupload\traits\Plupload;, (*9)

class Html extends BaseHtml { // adding this trait allow easy access to plupload use Plupload; }, (*10)


### Basic usage **Sample one** : one single file upload with basic UI and Automatic upload The controller file ``` php 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, (*11)

``` php, (*12)

// ..., (*13)

echo Html::asyncInput('demoUpload', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [ 'ui' => true, 'auto' => true, ]]); ?>, (*14)

//..., (*15)


**Sample two** : multi-file upload with basic UI and Automatic upload The controller file (*nothing was changed in the controller*) ``` php 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, (*16)

``` php, (*17)

// ..., (*18)

echo Html::asyncInput('demoUpload[]', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [ 'ui' => true, 'auto' => true, ]]); ?>, (*19)

//..., (*20)


**Config parameter** this parameter allow the developper to configure [plupload](http://www.plupload.com/docs/Options) 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 ``` php 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, (*21)

``` php namespace app\controllers;, (*22)

use app\models\Upload; use sweelix\yii2\plupload\components\UploadedFile; use yii\web\Controller; use Yii;, (*23)

/** * 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', ]; }, (*24)

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]);
}

}, (*25)


The index view file ``` php // ... [ 'ui' => true, 'auto' => true, ]]); ?> //...

Model usage with automatic file management

The model file, (*26)

``` php namespace app\models; use sweelix\yii2\plupload\behaviors\AutomaticUpload; use yii\db\ActiveRecord; use Yii;, (*27)

/** * Basic active record with uploadId (pkey autoincrement) and uploadFile (text) */ class Upload extends ActiveRecord { public static function tableName() { return '{{uploads}}'; }, (*28)

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'),
    ];
}

}, (*29)



The controller file ``` php 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, (*30)

``` php, (*31)

// ..., (*32)

[ 'ui' => true, 'auto' => true, ]]); ?>

//..., (*33)

```, (*34)

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., (*35)

Fork the project, create a feature branch , and send us a pull request., (*36)

The Versions

26/11 2015

dev-master

9999999-dev

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  Sources   Download

BSD-3-Clause

The Requires

 

by Philippe Gaultier

file yii asynchronous behaviors plupload

26/11 2015

dev-devel

dev-devel

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  Sources   Download

BSD-3-Clause

The Requires

 

by Philippe Gaultier

file yii asynchronous behaviors plupload

23/05 2014

1.0.3

1.0.3.0

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  Sources   Download

BSD-3-Clause

The Requires

 

by Philippe Gaultier

file yii asynchronous behaviors plupload

23/04 2014

1.0.2

1.0.2.0

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  Sources   Download

BSD-3-Clause

The Requires

 

by Philippe Gaultier

file yii asynchronous behaviors plupload

04/04 2014

1.0.1

1.0.1.0

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  Sources   Download

BSD-3-Clause

The Requires

 

by Philippe Gaultier

file yii asynchronous behaviors plupload

02/04 2014

1.0.0

1.0.0.0

PHP 5.4+ Sweelix Yii2 plupload extension - easy asynchronous file uploads

  Sources   Download

BSD-3-Clause

The Requires

 

by Philippe Gaultier

file yii asynchronous behaviors plupload