Display The Images From Protecte Images Folder

i wanna display the images in view file

my images folder kept in protected->images folder

how can i display the images from that folder

[indent] echo CHtml::image(Yii::app()->baseUrl.’/images/image_name.png’);


Yii::app()->baseUrl refers to application/protected folder

i am already trying like this but not displayed images

paste your image display code here

<?php echo CHtml::image(Yii::app()->request->baseUrl.’/images/’.$model->image,“image”,array(“width”=>200,“height”=>200)); ?>

plz check your image type either it is png or jpg etc…if $model->image don’t have a .png or .jpg etc type then try this one…

[indent]echo CHtml::image(Yii::app()->request->baseUrl.’/images/’.$model->image.’.png’,“image”,array(“width”=>200,“height”=>200));


replace png by your image type

If the images are saved under protected, surely you will get access denied using the method above.

You’ll have to render them using php, but if you’re rendering them publicly anyway, why save them under protected?

Check out CHttpRequests sendFile and xSendFile methods. The latter requires the mod_xsendfile extension though.

You’d use it like:

$fileName = Yii::getPathOfAlias('application.images').'/'.$imageName;


i don’t wanna give direct access to show my images from my folder with copy image url

please post the code how to display the image from protected folder

.please excuse me if i have any wrong in sentance

Well I’ll point you in the right direction with a simple example.

Create a controller (or action within an existing controller) that you can use as the url for the image:

public function actionShowimage($filename = false)


    if ($filename)


        $path =  Yii::getPathOfAlias('application.images'). '/';

        if (file_exists($path. $filename))




                file_get_contents($path. $filename)


        } else {

            echo "File not found!";




When you display the image in a view it should look something like this:

<img src="<?php echo Yii::app()->baseUrl; ?>/your-controller/showimage/?filename=yourimage.jpg" alt="" />

Bear in mind that you really need to use randomly generated filenames to have any sense of security, otherwise people could guess the filenames and view them using the same url.

ok thanks its working


sorry this code not displaying the image

controller not passing to the showimage method

my code in view

<img src="<?php echo Yii::app()->baseUrl; ?>/Users/showimage/?filename=<? echo $model->image;?>" alt="" />

where users is model

my controller is Userscontroller

public function actionShowimage($filename = false)


    if (&#036;filename)


    &#036;path =  Yii::getPathOfAlias('application.images'). '/';

    if (file_exists(&#036;path. &#036;filename))




            file_get_contents(&#036;path. &#036;filename)


    } else {

        echo &quot;File not found&#33;&quot;;




The code isn’t tested, I just wrote a quick example to show you how to go about it.

Make sure you actually allow this action for guests in your rules.

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