display blob image from database

i tried the wiki code form display image from database but i am getting this error when click the link to show image

the error is

Error 403

You are not authorized to perform this action.

my code is :—

in _view

<? echo CHtml::link(‘image’,array(‘displaySavedImage’,‘id’=>$data->emailId)); ?>

in controller is

public function actionDisplaySavedImage()

{echo &quot;hello control&quot;;


    header('Pragma: public');

    header('Expires: 0');

    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

    header('Content-Transfer-Encoding: binary');

    header('Content-length: '.&#036;model-&gt;file_size);

    header('Content-Type: '.&#036;model-&gt;file_type);

    header('Content-Disposition: attachment; filename='.&#036;model-&gt;file_name);

    echo &#036;model-&gt;file_content;


thank you in advance plz help.

echo "hello control";

what does it do ? i think this useless "echo" will destroy the image data structure,

and do you save you file data correctly ?

// just for reference  

// Bail out if the file isn’t really an upload.

if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {

exit('There was no file uploaded!');


$uploadfile = $_FILES['upload']['tmp_name'];

$uploadname = $_FILES['upload']['name'];

$uploadtype = $_FILES['upload']['type'];

$uploaddesc = $_POST['desc'];

// Open file for binary reading ('rb')

$tempfile = fopen($uploadfile, 'rb');

// Read the entire file into memory using PHP's

// filesize function to get the file size.

$filedata = fread($tempfile, filesize($uploadfile));

// Prepare for database insert by adding backslashes

// before special characters.

$filedata = addslashes($filedata);

// Create the SQL query.

$sql = "INSERT INTO filestore SET

filename = '$uploadname',

mimetype = '$uploadtype',

description = '$uploaddesc',

filedata = '$filedata'";

// Perform the insert.

$ok = @mysql_query($sql);

if (!$ok) {

exit('Database error storing file: ' . mysql_error());



try this code AS IS…and copy the error here again.

	public function actionDisplaySavedImage($id='')


   		if($id == null || trim($id)=='') {

      		echo "error in image, bad ID value [{$id}]";




   		if($model->file_content == null){

      		echo "error in image, using ID [{$id}] ";



      	header('Content-Type: '.$model->file_type);

      	echo $model->file_content;


ohh…important, try this code using this, directly on browser, to see what happens: