How To Display Image In Yii Framework From Database

hello everyone ,

                i have learned how to upload image in the database in this wiki   "http://www.yiiframework.com/wiki/2/". 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 http://www.yiiframework.com/wiki/95/saving-files-to-a-blob-field-in-the-database/

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

				'actions'=>array('index','view'),

				'users'=>array('*'),

			)

}

But already that code is present


	public function accessRules()

	{

		return array(

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

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

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

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

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

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

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()

		{

			$model=$this->loadModel($_GET['id']);

		 

			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);


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

		{


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

			$list = $_POST['Students'];

			

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

			 

			if($file=CUploadedFile::getInstance($model,'photo_data'))

       		 {

            $model->photo_file_name=$file->name;

            $model->photo_content_type=$file->type;

            $model->photo_file_size=$file->size;

            $model->photo_data=file_get_contents($file->tempName);

      		  }

 

			 

			

			if($model->save())

				$this->redirect(array('guardians/create','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}

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