PHP code example of sweelix / yii2-plupload

1. Go to this page and download the library: Download sweelix/yii2-plupload library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sweelix / yii2-plupload example snippets

 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

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

// ...

echo Html::asyncInput('demoUpload', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [
        'ui' => true,
        'auto' => true,
    ]]); 
 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');
    }
}
 php

// ...

echo Html::asyncInput('demoUpload[]', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [
        'ui' => true,
        'auto' => true,
    ]]); 
 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'),
        ];
    }
}
 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) {
            // ... 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]);
    }
}
 php

// ...

 echo Html::activeAsyncInput($fileUpload, 'uploadFile', ['config' => [
        'ui' => true,
        'auto' => true,
    ]]); 
 php
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'),
        ];
    }
}
 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]);
    }
}
 php

// ...

 echo Html::activeAsyncInput($fileUpload, 'uploadFile', ['config' => [
        'ui' => true,
        'auto' => true,
    ]]);