Friday 7 February 2014

How to show protected images in Yii

"protected" folder are not accessible from the client browser. This prevents user to have access to important files. If you want to store images inside "protected" and want them to be accessible, you need to publish them using CAssetManager.

Example:
1
2
$path = Yii::app()->basePath.'/path-inside-protected';
$imgurl= Yii::app()->assetManager->publish($path);
Yii will then use the file as an asset, coping it to the "assets" folder, sibling to "protected". After that, you can just use the url returned on your HTML.
1
<img src="<?php echo $imgurl?>">