Action view issue when displaying images

I am creating a simple registration form with 3 text boxes and 1 drop down box. However the form has an image upload option. After following the instruction given in the YII wiki . I am able to successfully upload the image and my database is also making the entries. Secondly, localhost/(my app)/(controller)/loadimage&id=2 displays the image As given in the wiki page.

However, I get an “htmlspecialchars() [<a href=‘function.htmlspecialchars’>function.htmlspecialchars</a>]: Invalid multibyte sequence in argument” error when after CRUD create option the view action is invoked. For ex. localhost/(my app)/(controller)/view&id=2 gives me the error. Looking at the stack information



– /Applications/MAMP/htdocs/iow/protected/controllers/ReportController.php(55): CController->renderPartial("view", array("model" => Report))

50 */

51 public function actionView($id)

52 {

53 $this->renderPartial(‘view’,array(

54 ‘model’=>$this->loadModel($id),

55 ));

56 }


58 /**

59 * Creates a new model.

60 * If creation is successful, the browser will be redirected to the ‘view’ page.


unknown(0): ReportController->actionView("11")

#11 [/i]


Screen Shot 2012-08-24 at 2.04.47 PM.png

My view.php file looks like this







array('label'=&gt;'List Report', 'url'=&gt;array('index')),

array('label'=&gt;'Create Report', 'url'=&gt;array('create')),

array('label'=&gt;'Update Report', 'url'=&gt;array('update', 'id'=&gt;&#036;model-&gt;id)),

array('label'=&gt;'Delete Report', 'url'=&gt;'#', 'linkOptions'=&gt;array('submit'=&gt;array('delete','id'=&gt;&#036;model-&gt;id),'confirm'=&gt;'Are you sure you want to delete this item?')),

array('label'=&gt;'Manage Report', 'url'=&gt;array('admin')),



<h1>View Report #<?php echo $model->id; ?></h1>


$this->widget(‘zii.widgets.CDetailView’, array(
















)); ?>[/i]

My _view.php file looks like this

[i]<div class="view">

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('id')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::link(CHtml::encode(&#036;data-&gt;id), array('view', 'id'=&gt;&#036;data-&gt;id)); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('registration')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::encode(&#036;data-&gt;registration); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('violation_type')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::encode(&#036;data-&gt;violation_type); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('city')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::encode(&#036;data-&gt;city); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('state')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::encode(&#036;data-&gt;state); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('description')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::encode(&#036;data-&gt;description); ?&gt;

&lt;br /&gt;

&lt;?php echo CHtml::image(Yii::app()-&gt;controller-&gt;createUrl('report/loadImage', array('id'=&gt;&#036;this-&gt;model-&gt;id)));?&gt;[/i]

I would be grateful, if some of you can advice how I should go about rendering the image along with form data as we see in the default view action case.

start commenting section by section from your view file and keep reproducing the error until you find the portion of code that is causing problems








That should prevent Yii from attempting to encode the binary data.

Hey keith,

Thanks. The view is rendering now. Howver, All I see is garbled data in the coloum that should show the image.


Screen Shot 2012-08-24 at 3.19.37 PM.png

Whereas controler/loadimage&11 shows the image.


Screen Shot 2012-08-24 at 3.21.42 PM.png

Yep, that makes sense because the browser doesn’t know what to do with the binary data. If you don’t need to show it in that view, your best bet is probably to remove that column.

It’s easier to store images on the file system and store just the file path in the database. That way you can render an img tag to show the image anywhere you like.

If you’re intent on keeping the binary data in the database, you’ll probably need to make a special controller and action to output the image data and point your img tag’s url to that. In that case, you’ll need to take charge of ensuring the correct headers are sent for the file type to ensure that the browser displays the image correctly and doesn’t attempt any sort of file download. I take it this is the purpose of your loadimage action.