Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:yii2:file_upload [2018/02/28 16:33]
smayr [Model]
systems:yii2:file_upload [2018/03/01 16:22] (current)
smayr [View]
Line 468: Line 468:
  
 == Example Image Upload and Save Filename in DB == == Example Image Upload and Save Filename in DB ==
 +
 +=== Model ===
 +In the model, override ''afterSave()'' to store image file to server, and its path in the database. Eg: ''@app/models/Organization.php'':
 +<code php>
 +public function afterSave($insert, $changedAttributes)
 +{
 +    if(isset($this->logo)) {
 +        $this->logo = UploadedFile::getInstance($this, 'logo');
 +        if(is_object($this->logo)) {
 +            $path = Yii::$app->basePath . '/images/';                  // set directory path to save image
 +            $this->logo->saveAs("{$path}{$this->id}_{$this->logo}");   // saving image in folder
 +            $this->logo = "{$this->id}_{$this->logo}";                 // appending id to image name            
 +            \Yii::$app->db->createCommand()
 +                  ->update('organization', ['logo' => $this->logo], 'id = "'.$this->id.'"')
 +                  ->execute();    // manually update image name to db
 +        }
 +    }
 +}
 +</code>
  
 === Controller === === Controller ===
  
-Add action ''actionCreate()'' to model (here it is model ''organization''), and override ''afterSave()'' to store image file to server, and its path in the database:+Add action ''actionCreate()'' to controller. Eg: ''@app/controllers/OrganizationController.php'':
 <code php> <code php>
 public function actionCreate() public function actionCreate()
Line 485: Line 504:
   }   }
 } }
 +</code>
  
-public function afterSave($insert, $changedAttributes) +=== View === 
-+<code php
-    if(isset($this->logo)) { +<?$form->field($model, 'logo')->fileInput(['class'=>'form-control']?>
-        $this->logo = UploadedFile::getInstance($this, 'logo')+
-        if(is_object($this->logo)) { +
-            $path = Yii::$app->basePath . '/images/';               // set directory path to save image +
-            $this->logo->saveAs($path.$this->id."_".$this->logo);   // saving image in folder +
-            $this->logo = $this->id."_".$this->logo;                // appending id to image name             +
-            \Yii::$app->db->createCommand() +
-                  ->update('organization', ['logo' => $this->logo], 'id = "'.$this->id.'"') +
-                  ->execute();    // manually update image name to db +
-        } +
-    } +
-}+
 </code> </code>
  
Line 507: Line 516:
  
 === Model === === Model ===
 +File ''@app/models/ImageFileUpload.php'':
 <code php> <code php>
 <?php <?php
Line 654: Line 664:
 === Controller === === Controller ===
  
 +File ''@app/controllers/ItemController.php'':
 <code php> <code php>
 //... //...
Line 731: Line 742:
 === View === === View ===
  
-File ''upload-image'':+File ''@app/views/item/upload-image'':
 <code php> <code php>
 <?php <?php
Line 770: Line 781:
 </code> </code>
  
-File ''view.php'' to call ''uplolad-image'' action:+File ''@app/views/item/view.php'' to call ''@app/views/item/upload-image.php'' action:
 <code php> <code php>
 <?php <?php
Line 817: Line 828:
   </div>   </div>
 </div> </div>
 +</code>
 +
 +Or simply use the Yii modal helper:
 +<code php>
 +<?php
 +    $itemImage = (!empty($model->image_file) ? 
 +        Yii::$app->homeUrl . 'img/data/' . Yii::$app->controller->id . "/{$model->image_file}"
 +        ''
 +    );
 +
 +    // Image Thumbnail
 +    if(!empty($itemImage)) {
 +        echo "<img class='img-thumbnail' src='{$itemImage}' alt='Item Image'>";
 +    } else {
 +        echo "<img class='img-thumbnail' src='data:image/png;base64," . base64_encode(\app\models\Tool::generateEmptyPng()) . "'/>";
 +    }
 +    
 +    echo Html::a('<i class="fa fa-plus" aria-hidden="true"></i> ' . Yii::t('app', 'Add Image'), 
 +        ['upload-image', 'id' => $model->id], 
 +        ['class' => 'btn btn-default']
 +    );
 +
 +    // Modal Window
 +    yii\bootstrap\Modal::begin([
 +        'header' => '<h4>Item Image</h4>',
 +        'size'   => yii\bootstrap\Modal::SIZE_LARGE,
 +        'toggleButton' => [
 +            'label' => '<i class="fa fa-image" aria-hidden="true"></i> ' . Yii::t('app', 'View Image'), 
 +            'class' => 'btn btn-default'
 +        ],
 +    ]);
 +
 +    if(!empty($itemImage)) {
 +        echo "<img class='img-fluid' src='{$itemImage}' alt='Item Image'>";
 +    } else {
 +        echo "<img class='img-fluid' src='data:image/png;base64," . base64_encode(\app\models\Tool::generateEmptyPng()) . "'/>";
 +    }
 +    echo '<div class="modal-footer">';
 +    echo '    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>';
 +    echo '</div>';
 +
 +    yii\bootstrap\Modal::end();  
 +?>
 </code> </code>