PHP code example of zozoh94 / yii2-filemanager

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

    

zozoh94 / yii2-filemanager example snippets


'modules' => [
    'filemanager' => [
        'class' => 'zozoh94\filemanager\Module',
        
        // Rename files
        'rename' => false,
        
        // Upload routes
        'routes' => [
            // Base web directory url
			'basePath' => '@webroot',
			
			// Path for uploaded files in web directory
			'uploadPath' => 'uploads',
			
			// Directory format for uploaded files.
			'dateDirFormat' => 'Y/m',
			
			// Thumbs directory template. Path, where thumb files are located
			'thumbsDirTemplate' => '{uploadPath}/{dateDirFormat}',
        ],
        
        // Thumbnails info
        'thumbs' => [
            'small' => [
				'name' => 'Small size',
				'size' => [120, 80],
			],
			'medium' => [
				'name' => 'Medium size',
				'size' => [400, 300],
			],
			'large' => [
				'name' => 'Large size',
				'size' => [800, 600],
			],
        ],
    ],
],

'routes' => [
	// Base web directory url
	'basePath' => '@webroot',
	
	// Path for uploaded files in web directory
	'uploadPath' => 'uploads',
	
	// Directory format for uploaded files.
	'dateDirFormat' => 'Y/m',
	
	// Thumbs directory template. Path, where thumb files are located
	'thumbsDirTemplate' => '{uploadPath}/{dateDirFormat}',
],

'basePath' => '@webroot'

'basePath' => '@frontend/web'

'basePath' => 'system/upload/directory'

'uploadPath' => 'uploads'

'uploadPath' => 'files/upload'

'dateDirFormat' => 'Y/m'

'dateDirFormat' => 'Y/m/d'

'dateDirFormat' => 'Y-m'

'thumbsDirTemplate' => '{uploadPath}/{dateDirFormat}'

'thumbsDirTemplate' => '{uploadPath}/thumbs/{dateDirFormat}'

'thumbsDirTemplate' => '{uploadPath}/{dateDirFormat}/thumbs'

'thumbs' => [
	'small' => [
		'name' => 'Small size',
		'size' => [120, 80],
	],
	'medium' => [
		'name' => 'Medium size',
		'size' => [400, 300],
	],
	'large' => [
		'name' => 'Large size',
		'size' => [800, 600],
	],
],

'thumbs' => [
	'<alias>' => [
		'name' => 'Name of alias',
		'size' => ['<width>', '<height>'],
	],
],

'rename' => true

'autoUpload' => true

use zozoh94\filemanager\widgets\FileInput;

echo $form->field($model, 'original_thumbnail')->widget(FileInput::className(), [
    'buttonTag' => 'button',
    'buttonName' => 'Browse',
    'buttonOptions' => ['class' => 'btn btn-default'],
    'options' => ['class' => 'form-control'],
    
    // Widget template
    'template' => '<div class="input-group">{input}<span class="input-group-btn">{button}</span></div>',
    
    // Optional, if set, only this image can be selected by user
    'thumb' => 'original',
    
    // Optional, if set, in container will be inserted selected image
    'imageContainer' => '.img',
    
    // Default to FileInput::DATA_URL. This data will be inserted in input field
    'pasteData' => FileInput::DATA_URL,
    
    // JavaScript function, which will be called before insert file data to input.
    // Argument data contains file data.
    // data example: [alt: "Four cats", description: "123", url: "/uploads/2014/12/cats-100x100.jpeg", id: "45"]
    'callbackBeforeInsert' => 'function(e, data) {
        console.log( data );
    }',
]);

echo FileInput::widget([
    'name' => 'mediafile',
    'buttonTag' => 'button',
    'buttonName' => 'Browse',
    'buttonOptions' => ['class' => 'btn btn-default'],
    'options' => ['class' => 'form-control'],
    
    // Widget template
    'template' => '<div class="input-group">{input}<span class="input-group-btn">{button}</span></div>',
    
    // Optional, if set, only this image can be selected by user
    'thumb' => 'original',
    
    // Optional, if set, in container will be inserted selected image
    'imageContainer' => '.img',
    
    // Default to FileInput::DATA_IDL. This data will be inserted in input field
    'pasteData' => FileInput::DATA_ID,
    
    // JavaScript function, which will be called before insert file data to input.
    // Argument data contains file data.
    // data example: [alt: "Four cats", description: "123", url: "/uploads/2014/12/cats-100x100.jpeg", id: "45"]
    'callbackBeforeInsert' => 'function(e, data) {
        console.log( data );
    }',
]);

use zozoh94\filemanager\widgets\TinyMCE;

<?= $form->field($model, 'content')->widget(TinyMCE::className(), [
    'clientOptions' => [
           'language' => 'ru',
        'menubar' => false,
        'height' => 500,
        'image_dimensions' => false,
        'plugins' => [
            'advlist autolink lists link image charmap print preview anchor searchreplace visualblocks code contextmenu table',
        ],
        'toolbar' => 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code',
    ],
]); 

use zozoh94\filemanager\behaviors\MediaFileBehavior;

public function behaviors()
{
    return [
        'mediafile' => [
            'class' => MediaFileBehavior::className(),
            'name' => 'post',
            'attributes' => [
                'thumbnail',
            ],
        ]
    ];
}

use zozoh94\filemanager\models\MediaFile;

$model = Post::findOne(1);
$mediaFile = MediaFile::loadOneByOwner('post', $model->id, 'thumbnail');

// Ok, we have mediafile object! Let's do something with him:
// return url for small thumbnail, for example: '/uploads/2014/12/flying-cats.jpg'
echo $mediaFile->thumbFiles->getUrl('small');

php composer.phar