display image from database using CDetailView

Referring to How to upload file to database , I can upload a ‘IMAGE’ file to database.

But how to display it in CDetailView. It has formatter ‘image’ supported, but it requires url of the file!

And I will be having it in longblob converted to string( in php ) .

Is it possible ? or I will have to use normal html to render image?

It is possible. try ‘type’=>‘html’

html: the attribute value will be purified and then returned. (CFormatter)

This isn’t my solution! Well, I figured out the solution.

I created a controller action which on giving id, displays image from database. Now just have to give url i guess to CDetailView!

Rohit, would you mind posting some simple samples of your solution. I am facing this exact same issue.

Here it goes :)

PropertyImageController has action view, in which i render a view which has,


header("Content-type: ".$model->image_type);

echo $model->image_data;

in a views/propertyImage/view.php

in a controller i change property_image_id to url








and now its just a matter of giving a url to CDetailView :)


      'label'=>'Property Image',




as there is no field in my property model which holds url, it holds id of property Image. So, I had to use the same variable to show image :)

Hope this helps. and if you can make it more simple, please go ahead and suggest…

Thanks for showing this, great place for me to start. -later

thank you rohit

hi rohit…

am very new to yii… am also facing this problem. now i need a solution. On controller were we use this code… plz help me out.

in a controller i change property_image_id to url







Hello rohit.

saving image into database is not a good idea at all. though this feature was supported it will affect your database performance instead of saving the image directly into the database you can save the image into the file system and store the image name to the db. this will highly reduce you database performance issue.