images

Hello i’m very newbie in php and Yii…

I want to ask about what is the best to save image as path name or as BLOB in DB …?

And is there any example to do that in Yii frame work…

Or any thing clue… i will appreciate it…

Thanks a lot

Saving images should be done on the file system, Not the DB. You should store the image location in the DB to know were it’s located and fetch it for display. Yii has a CFileUpload component that you can use to upload files, The rest is done by you.

Hi.

I don’t know what (and if exist) a best system, but I use blob in DB.

This is very comfortable from many point of view, for example is authomatically deleted when the record id deleted (even by foreign keys).

For save an image you can use CHtml::CFileUpload and CUploadedFile, and in the controller (or model) do something like




if($file=CUploadedFile::getInstance($image,'file'))

{

	$image->data=file_get_contents($file->tempName);

	$image->mime=$file->type;

}



Where data is the blob, and mime a text field for the mime type.

For show an image in browser, you can create an action in controller like that:




public function actionDisplay()

{

	$model=$this->loadModel();

	header('Content-Type: '.$model->mime);

	echo $model->data;

}




Don’t want to start a discussion there about what way of storing images is better. I just prefer to store filenames or, if an object can have only one .jpg picture, don’t store anything in the db, but just use naming convention (like “objectId.jpg”).

Read this cookbook’s page first: http://www.yiiframework.com/doc/cookbook/2/

Also, instead of using CUploadedFile.saveAs method you can use EasyPhpThumb extension to change the source image (resize, crop etc.).

Thanks all who have give me a respond but i want to ask about what i have done.

This doesn’t mean i’m lazy but i really still learn hardly. Thanks before…

I just design like below for testing porpose only,

In my DB i have design

-ID auto increment

-Path as varchar

-Gambar as BLOB

This is in my model

public function rules()

{

   return array(


          array('Path', 'length', 'max'=>200),


          array('Gambar', 'safe'),


          array('Gambar', 'file', 'types'=>'jpg, gif, png'),


          array('ID, Path, Gambar', 'safe', 'on'=>'search'),


         );

}

And this is in the Controller :

public function actionCreate()

{

	$model=new ImageTrial;


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


	{


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


                    $model->Gambar = CUploadedFile::getInstance($model,'Gambar');





  			if($model->save())


                    {


                        $model->Gambar->saveAs('path/to/localFile');


                        


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


                    }


	}





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


		'model'=>$model,


	));


}

I still can’t make : saving an image into DB .

So can anyone give a feedback … ? (Thanks a lot before

And i have a question about : $model->Gambar->saveAs(‘path/to/localFile’);

‘path/to/localFile/’ => what this section means. .?

I just tried this as my trial after i read : http://www.yiiframework.com/doc/cookbook/2/revision/2/

So i have a lot of mistake can anyone help me… ? please…

Thanks a lot

Gbu