How to force download a file??

Hello, How can i force download a file from Yii ??

this is not working

$file = $filepath . $filename;





 //CREATE/OUTPUT THE HEADER


 header("Content-type: application/force-download");


 header("Content-Transfer-Encoding: Binary");


 header("Content-length: ".filesize($file));


 header("Content-disposition: attachment; filename=\"".basename($file)."\"");


 readfile($file);

Please help.

What is it doing exactly?

You really want to look at readfile:

http://php.net/manual/en/function.readfile.php

The first example should do the trick.


$file = 'monkey.gif';


if (file_exists($file)) {

    header('Content-Description: File Transfer');

    header('Content-Type: application/octet-stream');

    header('Content-Disposition: attachment; filename='.basename($file));

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

    header('Expires: 0');

    header('Cache-Control: must-revalidate');

    header('Pragma: public');

    header('Content-Length: ' . filesize($file));

    ob_clean();

    flush();

    readfile($file);

    exit;

}

There is also the Yii method sendFile() but I haven’t used this myself:

http://www.yiiframework.com/doc/api/1.0/CHttpRequest#sendFile-detail

I really needed this and I solved installing the "cfile" extension (check the extension page)

$file= "path to file"

    $myfile = Yii::app()->file->set($file);


    $myfile->download($file, false);


    


    $this->redirect(Yii::app()->request->urlReferrer);