How to stream a file to download?

Hello,

I have a file upload feature (behind a login), and I now want user to allow them to download the files (after login). How to do this?

Sorry, find the answer myself:

create an actionDownload:

public function actionDownload()


{


	$model=$this->loadModel();


	$fname = dirname(__FILE__).'/../uploads/'.$model->id.'_'.$model->filename;


	$this->renderPartial('download', array('fname'=>$fname, 'lname'=>$model->filename,'ctype'=>$model->contentType), false, true);


}

with this in download.php:

<?php

header('Content-type: '.$ctype);

header(‘Content-Disposition: attachment; filename="’.$lname.’"’);

readfile($fname);

?>

Why don’t you put download.php code into the action? It just seems more logical to me since there is nothing to show to a user.

Hello Guys,

I used this download code and got this error,

PHP Error

Header may not contain more than a single header, new line detected.

/../..//download.php(3)

1 <?php

2 header('Content-type: '.$ctype);

3 header(‘Content-Disposition: attachment; uploadedFile="’.$lname.’"’);

4 readfile($fname);

5 ?>

Here is my controller




public function actionDownload()

{

	    

       $model=$this->loadModel();

       $fname = $model->upload_fileName;

	$this->renderPartial('download', array('fname'=>$fname, 'lname'=>$model->upload_fileName,'ctype'=>$model->mime_type), false, true);

			

			

			

	

	}




try this

header(‘Content-Disposition: attachment; filename="’.$lname.’"’);

Same Error message I got

PHP Error

Header may not contain more than a single header, new line detected.

/../../../download.php(3)

1 <?php

2 header('Content-type: '.$ctype);

3 header(‘Content-Disposition: attachment; uploadedFile="’.$lname.’"’);

4 readfile($fname);

5 ?>

If you have access to your server config and can install webserver modules, you can also check out CHttpRequest::xSendFile. Saves you some of the header hassle. I wouldn’t use a view file for this either, you are not rendering a view here. Instead put the output logic right into your controller action.

if code for a file download would be placed in you action then it would be little bit easy to handle this prob…