Both sides previous revision
Previous revision
Next revision
|
Previous revision
|
systems:yii2:file_upload [2018/02/28 17:54] smayr [Example Image Upload and Save Filename in DB] |
systems:yii2:file_upload [2018/03/01 16:22] (current) smayr [View] |
$this->logo = UploadedFile::getInstance($this, 'logo'); | $this->logo = UploadedFile::getInstance($this, 'logo'); |
if(is_object($this->logo)) { | if(is_object($this->logo)) { |
$path = Yii::$app->basePath . '/images/'; // set directory path to save image | $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->saveAs("{$path}{$this->id}_{$this->logo}"); // saving image in folder |
$this->logo = "{$this->id}_{$this->logo}"; // appending id to image name | $this->logo = "{$this->id}_{$this->logo}"; // appending id to image name |
\Yii::$app->db->createCommand() | \Yii::$app->db->createCommand() |
->update('organization', ['logo' => $this->logo], 'id = "'.$this->id.'"') | ->update('organization', ['logo' => $this->logo], 'id = "'.$this->id.'"') |
</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]] |
</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> |