PHP code example of floor12 / yii2-module-files

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

    

floor12 / yii2-module-files example snippets

  
 'modules' => [
             'files' => [
                 'class' => 'floor12\files\Module',
                 'storage' => '@app/storage',
                 'cache' => '@app/storage_cache',
                 'token_salt' => 'some_random_salt',
             ],
         ],
     ...
 
 
  public function behaviors()
  {
      return [
          'files' => [
              'class' => 'floor12\files\components\FileBehaviour',
              'attributes' => [
                  'avatar',
                  'documents'
              ],
          ],
          ...
 

  public function attributeLabels()
     {
         return [
             ...
             'avatar' => 'Аватар',
             'documents' => 'Документы',
             ...
         ];
     }
 

 public function rules()
 {
     return [
         // Avatar is llow to uploade 1 file with this extensions: jpg, png, jpeg, gif
         ['avatar', 'file', 'extensions' => ['jpg', 'png', 'jpeg', 'gif'], 'maxFiles' => 1], 
 
         // Documens allows to upload a few files with this extensions: docx, xlsx
         ['documents', 'file', 'extensions' => ['docx', 'xlsx'], 'maxFiles' => 10],
     ...    
 

 // The href field contains web path to file source
 echo Html::img($model->avatar->href)     
 
 // __toString() method of File object will return href as well
 echo Html::img($model->avatar)          
 

 // User avatar thumbnail with 200px width 
 echo Html::img($model->avatar->getPreviewWebPath(200));     
 
 // User avatar thumbnail with 200px width  and WEBP format
 echo Html::img($model->avatar->getPreviewWebPath(200, 0, true));     
      
 

 foreach ($model->docs as $doc}
     Html::a($doc->title, $doc->href);
 

 <picture>
     <source type="image/webp" media='(min-width: 500px)' srcset="
                              <?= $model->poster->getPreviewWebPath(150, true) 

echo PictureWidget::widget([
    'model' => $file,
    'alt' => 'Some alternative text',
    'width' => 100
]);

 echo \floor12\files\components\FileListWidget::widget([
     'files' => $model->docs, 
     'downloadAll' => true, 
     'zipTitle' => "Documents of {$user->fullname}" 
 ]) 
 

<?= $form->field($model, 'avatar')->widget(floor12\files\components\FileInputWidget::class) 
html

<picture>
    <source type="image/webp"
            srcset="/imageurl/image1xWebp 1x,/imageurl/image2xWebp 2x">
    <img src="/imageurl/image 1x"
         srcset="/imageurl/image 1x,/imageurl/image 2x"
         alt="<?= $model->title