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