PHP code example of davidxu / yii2-dropzone

1. Go to this page and download the library: Download davidxu/yii2-dropzone 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/ */

    

davidxu / yii2-dropzone example snippets



use davidxu\dropzone\Dropzone;
use yii\helpers\Url;

// Important: register DropzoneAsset first on page
use davidxu\dropzone\assets\DropzoneAsset;
DropzoneAsset::register($this);
// without ActiveForm
echo Dropzone::widget([
    'model' => $model,
    'attribute' => 'image_src',
    'name' => 'image_src', // If no model and attribute pointed
    'url' => Url::to('@web/upload/local'),
    'maxFiles' => 3,
    'acceptedFiles' => 'image/*',
    'uploadBasePath' => 'uploads/',
    // for single file,
    'existFiles' => [
        'id' => 1,
        'name' => 'some_name.jpg',
        'path' => 'some_path_for_file',
        'size' => 1111,
    ],
    // for multiple files
//    'existFiles' => [
//        [
//            'id' => 1,
//            'name' => 'some_name.jpg',
//            'path' => 'some_path_for_file',
//            'size' => 1111,
//        ], [
//            'id' => 2,
//            'name' => 'some_other_name.jpg',
//            'path' => 'some_path_for_other_file',
//            'size' => 2222,
//        ],
//    ],
    'storeInDB' => true, // return file id in DB to image url instead of file url if true, migrate model db first. default false
    'metaData' => ['foo' => 'bar',],
    'crop' => true, // default false, if true, the 'maxFiles' will be forced to 1
]); 

use davidxu\adminlte4\actions\LocalAction;
use davidxu\adminlte4\models\Attachment;
use yii\web\Controller;

class UploadController extends Controller
{
    public function actions(): array
    {
        $actions = parent::actions();
        return ArrayHelper::merge([
            'local' => [
                'class' => LocalAction::class,
                'url' => Yii::getAlias('@web/uploads'), // default: '@web/uploads'. stored file base url,
                'fileDir' => Yii::getAlias('@webroot/uploads'), // default: '@webroot/uploads'. file store in this dirctory,
                'allowAnony' => true, // default false
                'attachmentModel' => Attachment::class,
            ],
        ], $actions);
    }
}


use davidxu\dropzone\Dropzone;
use davidxu\adminlte4\enums\QiniuUploadRegionEnum;
use davidxu\adminlte4\enums\UploadTypeEnum;
use yii\helpers\Url;
// Important: register DropzoneAsset first on page
use davidxu\dropzone\assets\DropzoneAsset;
DropzoneAsset::register($this);

echo Dropzone::widget([
    'model' => $model,
    'attribute' => 'image_src',
    'name' => 'image_src', // If no model and attribute pointed
    'url' => QiniuUploadRegionEnum::getValue(QiniuUploadRegionEnum::EC_ZHEJIANG_2),
    'drive' => UploadTypeEnum::DRIVE_QINIU,
    // ...... (refer to local config in view)
]); 

use davidxu\adminlte4\actions\QiniuAction;
use davidxu\adminlte4\models\Attachment;
use yii\web\Controller;
use yii\web\BadRequestHttpException;

class UploadController extends Controller
{
     /**
     * @throws BadRequestHttpException
     */
    public function beforeAction($action): bool
    {
        $currentAction = $action->id;
        $novalidateActions = ['qiniu'];
        if(in_array($currentAction, $novalidateActions)) {
            // disable CSRF validation
            $action->controller->enableCsrfValidation = false;
        }
        parent::beforeAction($action);
        return true;
    }
    public function actions(): array
    {
        $actions = parent::actions();
        return ArrayHelper::merge([
            'qiniu' => [
                'class' => QiniuAction::class,
                'url' => Yii::getAlias('@web/uploads'), // default: '@web/uploads'. stored file base url,
                'allowAnony' => true, // default false
                'attachmentModel' => Attachment::class,
            ],
        ], $actions);
    }
}

php composer.phar