Skip to content

Instantly share code, notes, and snippets.

View maximishchenko's full-sized avatar

Maxim Ishchenko maximishchenko

View GitHub Profile
@maximishchenko
maximishchenko / YII_single_file_upload_create_and_update.php
Last active September 17, 2015 21:43
Yii upload single file (action "create" and "update")
<?
// There and next:
// webroot.images - path to images forlder (such as Yii::app()->baseurl."/images/")
// if using another forder name (for example: /upload/) - Yii::getPathOfAlias('webroot.upload')
// file_field_name - db field that contains name of uploaded file
// DIRECTORY_SEPARATOR - separator, such as "/" or included folders
// PATH_TO_IMAGE_FOLDER - full path to folder with images
// Previously added CImageHandler and Fancybox extensions
// CImageHandler:
@maximishchenko
maximishchenko / Yii_delete_file_when_delete_record.php
Last active September 17, 2015 18:30
Yii delete uploaded file when delete record from database
<?php
// Add to current model
protected function beforeDelete()
{
if (parent::beforeDelete()) {
// file_field_name - db field that contains name of uploaded file
if ($this-><file_field_name>)
// webroot.images - path to images forlder (such as Yii::app()->baseurl."/images/")
// if using another forder name (for example: /upload/) - Yii::getPathOfAlias('webroot.upload')
@unlink(Yii::getPathOfAlias('webroot.images') . "DIRECTOTY_SEPARATOR" . $this-><file_field_name>);
@maximishchenko
maximishchenko / YII_photo_nophoto.php
Created September 17, 2015 17:17
Yii if image exist show image else show nophoto.png
<?php
echo !empty($model-><field>) && file_exists(Yii::getPathOfAlias('webroot.images')."<PATH>".$model-><field>) ? CHtml::image(Yii::app()->baseurl."<PATH>".$model-><field>,"",array("style"=>"width:50px;height:50px;align:center;")) : CHtml::image(Yii::app()->baseurl."/<PATH>/no_img.png","",array("style"=>"width:50px;height:50px;align:center;"));
?>