How To Display Image In Yii Framework From Database

hello everyone ,

                i have learned how to upload image in the database in this wiki   "". i have uploaded it in the in database successfully.

but i dono how to display image . it displays only the name of image

[b]getAttributeLabel('image')); ?>:[/b]

	image); ?>


please help me . thank you in Advance

Hello and welcome to the framework. You should really try to read "The Definitive Guide to Yii" and understand, if not all then most of it. If you did that you would probably be able to create this yourself, or you should be able to search and understand the work of others.

But now, because i’m such a nice guy, i will give an idea to how it could be made (i’m not sure of it, and i haven’t tested it)

<?php $image = getAttributeLabel('image')); ?>

<img src="<?php echo Yii::app()->request->baseUrl.'/images/subfolder/'.$image;?> " />

Are you using BLOB ie save image in DB

Like this

hey thanks i have seen the link and modified my code but when i tried to view the image it says "

Error 403

[color=#555555][font=Arial, Helvetica, sans-serif][size=2]You are not authorized to perform this action.[/size][/font][/color]"

i’m sorry fonis i’m just a newb to this framework ya sure ill go through it . but i’m using database blob data type to save and retrieve it

Add your Controller action to

public function accessRules()


		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions





But already that code is present

	public function accessRules()


		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions




			array('allow', // allow authenticated user to perform 'create' and 'update' actions




			array('allow', // allow admin user to perform 'admin' and 'delete' actions




			array('deny',  // deny all users





in view

to see the uploaded image

echo '<img class="imgbrder" src="'.$this->createUrl('DisplaySavedImage&id='.$model->primaryKey).'" alt="'.$model->photo_file_name.'" width="101" height="107" />';

in controller

public function actionDisplaySavedImage()




			header('Pragma: public');

			header('Expires: 0');

			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

			header('Content-Transfer-Encoding: binary');

			header('Content-length: '.$model->photo_file_size);

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

			header('Content-Disposition: attachment; filename='.$model->photo_file_name);


				//echo '<img src="'.base64_decode($model->photo_data).'" />';

				echo $model->photo_data;


in actionCreate

public function actionCreate()


		$model=new Students;

		// Uncomment the following line if AJAX validation is needed

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




			$list = $_POST['Students'];





















thanks buddy i solved and its working fine

1 Like

:) fine

thanks… its working… can you help me for image update. i want to show previous image when i ll go for update