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 17:54]
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 ===
Line 487: Line 506:
 </code> </code>
  
 +=== View === 
 +<code php> 
 +<?= $form->field($model, 'logo')->fileInput(['class'=>'form-control']) ?> 
 +</code>
  
 Source: [[http://stackoverflow.com/questions/28425100/yii-2-upload-and-save-image-files-locally-and-image-url-is-saved-in-db|StackOverflow: Yii 2 Upload and Save Image Files Locally and Image URL is Saved in DB]] Source: [[http://stackoverflow.com/questions/28425100/yii-2-upload-and-save-image-files-locally-and-image-url-is-saved-in-db|StackOverflow: Yii 2 Upload and Save Image Files Locally and Image URL is Saved in DB]]
Line 806: 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>