Yii sendFile - issue downloading images

Hi,

I am trying to send images from the server to user using the sendFile function.

The Open With/Save dialog box pops up and asks to open/save the file but but it does not seem to be sending the file. When I try to open the image with my image browser app it has an error saying "The file does not appear to be a GIF." The size of the file in the property is 47 bytes which is the exact size of the full path string of the directory/file.

The images are stored in the file system and the file name, mime type and size are stored in a database table. I have no trouble displaying the image on a page, just downloading from filesystem is an issue.

My Controller:




.

.

public function actionDownloadImage()

{

  $model=$this->loadModel($_GET['id']);	

  $fileDir=Yii::app()->request->baseUrl.'/images/Promotional/';

  Yii::app()->request->sendFile(

    $model->lg_image_name,

    $fileDir.$model->lg_image_name,

    $model->lg_image_mime

  );	  

}

.

.



My Download Link:




.

.

<div class="view" id="promotional">

  <div class="item-image">

    <?php 

      echo CHtml::link("Download Image", array(

        'downloadImage', 'id'=>$data->id));

    ?>

  </div>

</div>

.

.



My downloadImage page:




<?php 

  $this->actionDownloadImage(); 

?>



Any help appreciated.

Thanks

Second parameter of CHttpRequest::sendFile() should be the content of the file - not the path to the file:




Yii::app()->request->sendFile(

	$model->lg_image_name,

	file_get_contents($fileDir . $model->lg_image_name),

	$model->lg_image_mime

);



Thanks for the reply, but if I use:




public function actionDownloadImage()

{

  $model=$this->loadModel($_GET['id']);	

  $fileDir=Yii::app()->request->baseUrl.'/images/Promotional/';

  Yii::app()->request->sendFile(

    $model->lg_image_name,

    file_get_contents($fileDir.$model->lg_image_name),

    $model->lg_image_mime

  );

}



I get the following error:

file_get_contents(/ngf/images/Promotional/BananaAppleCinnamon.gif) [<a href=‘function.file-get-contents’>function.file-get-contents</a>]: failed to open stream: No such file or directory

Thanks

Thanks for the help phtamas, you put me on the right track. I was not passing the full file location in the form of a URL to the file_get_contents() method.

If any body else happens to have this issue, read this article from stackoverflow: