Image Upload With Url & Save In Db

.—. .-. .-. .–. .-. .-…-. .-. .–. .----…-. .-. .—. .----.

/ }| {} | / {} \ | | ||| | / {} \ { { | `.’ |/ _}{ {

\ }| { } |/ /\ \| |\ || |\ |/ /\ \.-.} }| |\ /| |\ }.-.} }

---'-’ -'-’ -'-’ -'-’ -'-’ -'----’ -' -'—’ `----’

hey Yii… .i’m from srilanka &i’m newer on yii wall & i made imge upload programm …it upload image in localserver & save image in DB as URL this very advantage to reduce for DB weight

places make comment & add update for this & i apology if have any mistake …

IMP ;-

i make DB as cities & i generate model & crud using Gii

CREATE TABLE IF NOT EXISTS cities (

city_id int(60) NOT NULL AUTO_INCREMENT,

city_name varchar(255) NOT NULL,

Discription text NOT NULL,

city_laitute float NOT NULL,

city_lontute float NOT NULL,

image varchar(255) NOT NULL,

more text NOT NULL,

PRIMARY KEY (city_id),

UNIQUE KEY city_id (city_id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

this is my code for upload image URL

cities controller

public function actionCreate()

{


	$model=new Cities;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if(isset($_POST['Cities']))


	{


		$model->attributes=$_POST['Cities'];


		//image upload


		$myfile=CUploadedFile::getInstance($model,'image');


		if (is_object($myfile) && get_class($myfile)==='CUploadedFile') {


			// gimage upload path edite with your path 


			$model->image="http://10.0.2.2/sitename/images/cities/{$myfile->name}";


		}


		if($model->save())


			


			$myfile->saveAs(Yii::app()->basePath.'/../images/'.$myfile->name);//save image


			$this->redirect(array('view','id'=>$model->city_id));


	}





	$this->render('create',array(


		'model'=>$model,


	));


}

public function actionUpdate($id)


{


	$model=$this->loadModel($id);





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if(isset($_POST['Cities']))


	{


		$model->attributes=$_POST['Cities'];


		


		$myfile=CUploadedFile::getInstance($model,'image');


		if (is_object($myfile) && get_class($myfile)==='CUploadedFile') {


			// get image with image path


			$model->image="http://10.0.2.2/sitename/images/cities/{$myfile->name}";


		}


		if($model->save())


			$myfile->saveAs(Yii::app()->basePath.'/../images/'.$myfile->name);//save image


			$this->redirect(array('view','id'=>$model->city_id));


	}





	$this->render('update',array(


		'model'=>$model,


	));


}

cities model

add this 2 in to public function rules()

array(‘image’, ‘file’,‘types’=>‘jpg, gif, png’, ‘allowEmpty’=>true, ‘on’=>‘update’),

array(’ image’, ‘length’, ‘max’=>255, ‘on’=>‘insert,update’),

view

[b]

add this code in to form.php[/b]

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'cities-form',


'enableAjaxValidation'=&gt;false,


	'htmlOptions' =&gt; array(


			'enctype' =&gt; 'multipart/form-data',


	),

)); ?>

<div class="row">

   &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'image'); ?&gt;


	&lt;?php echo CHtml::activeFileField(&#036;model, 'image'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'image'); ?&gt;


&lt;/div&gt;

[color="#FF0000"][b]now you can upload image & save it as image URL.

IMP :-

add your image path whem add this code [/b][/color]

$model->image="http://10.0.2.2/sitename/image_folder_name/cSub_image_foldername(ifhave)/{$myfile->name}";

finaly output like this

my upload image name = colombo.jpg

this save DB as = http://10.0.2.2/TheGuidersite_service/images/cities/colombo.jpg

this method will be very important when you make web service

some one can tell how add thumbnails for this //////?????????????????

Check this Link

[/size]