Image Display

Hey guys I can’t get images to display. I store images in protected/images/ and save filenames in a database field.

This is my output with

<?php echo CHtml::image(Yii::app()->basePath.'/images/'.$data->image); ?>


Screen Shot 2012-08-31 at 4.47.31 AM.png

On the broken image picture if I copy the url link and past it in my browser I get a file not found error even though the images are there in the exact same directory.


Screen Shot 2012-08-31 at 4.44.47 AM.png

Please help. I need to show images in list view for each id.

By convention, the files under your WebRoot/protected are protected from being accessed by Web users.

You can store your images unser WebRoot/images (i.e., “idiotsonwheels/images” in your case). :)

protected folder is not accessible by the web server you should not use protected folder save your image or other assets use ROOT folder


So this is what I have done. CHanged code




Confirmed that /idiotonwheels/images exist. Nevertheless, I get this error

PHP warning

move_uploaded_file(/idiotsonwheels/images/12310843logo-2.png) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory


Whats going wrong here…

No you need to save to a path, not a URL. Use basePath(), but ensure the path that you save to is accessible. If you want it in an images directory, try something like:

$model->image->saveAs(Yii::getPathOfAlias('webroot.images') . '/' . $imageName);

You need to ensure that the images directory exists under your web root, not your application root.

Weird, but found the fix… Its better to use this getPathOfAlias rather than baseUrl. Wonder why ?


Because baseUrl() is giving you a URL, not a path.


At least the uploaded comes in application/images folder. However, I face the initial problem of displaying the image. This thing is making me crazy.


Screen Shot 2012-08-31 at 12.41.13 PM.png


Screen Shot 2012-08-31 at 12.41.36 PM.png


Screen Shot 2012-08-31 at 12.42.43 PM.png

Why can’t I display the image.

From view file.


echo CHtml::image(Yii::getPathOfAlias('webroot').'/images/'.$data->image); ?>

Is your web root pointing to htdocs or htdocs/idiotsonwheels? Try in your view echoing out the path, i.e.:

<?php echo Yii::getPathOfAlias('webroot').'/images/'.$data->image

In the view file you should be using a URL, not a path.

When you’re saving the image to the file system, you need to provide a path, so PHP knows where to put the image.

When outputting the image tag in a view, you need to provide a URL, so the user’s browser knows the address where the image is located.

You also need to ensure that the file is saved into a folder within your web root. Your application folder shouldn’t be accessible to web users, while your web root is.

My webroot is pointing to htdocs/idiotsonwheels . i.e the application folder. how can i change this?

Keith by webroot you mean the /idiotsonwheels/images

or htdocs/images

My webroot points to htdocs/idiotsonwheels/

So how do I move outside the application directory.

Just out of curiosity (and it’s obviously not a permanent solution) but does this work?

<?php CHtml::image('http://localhost:8888/idiotsonwheels/images/'.$data->image);

<?php echo CHtml::image(Yii::app()->baseUrl.'/images/'.$data->image);?>

As Keith has stated, a file should be accessed using "PATH" when you create/update/delete in your application, but it should be accessed using "URL" by your users using their browsers.