how to create form to download file?

hi,

I’m trying to make a form to download the dump file of database. I have successfully creating the file & locating it. But when I tried to download it, the file I’ve downloaded always be an empty file, it wasn’t the file I’ve created before. I’ve tried many methods but nothing works.

Here are some of methods I’ve tried…




//the first one

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, post-check=0, pre-check=0');

    header('Pragma: public');

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

    ob_clean();

    flush();

    readfile($file);

    exit;

}


//the second one

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

        $myfile->permissions=777;

        $myfile->download('file_backup_'.date('d-m-Y').'.txt', true);


//$myfile get the right file, because I get the right copy of file when I tried to copy it, below is the source to do copy

        $mynewfile = $myfile->copy('test2.txt');

//I got just the right result when I open test2.txt

for the second one I used a cfile extensions and here is how to import it


Yii::import('application.extensions.file.CFile');


//the third one 

Yii::app()->request->xSendFile($file, array(

        'saveName' => 'RequestedFile.txt',

   ));

those are all methods I have tried, but none of them is working…

please help me solve this problem…

any help would be appreciate… :)

thank you…

isn’t there anyone know how to solve this problem? :)

This wiki article can help you - http://www.yiiframework.com/wiki/129/x-sendfile-serve-large-static-files-efficiently-from-web-applications/

For the first method readfile() returns FALSE in case of an error… try to check for that… and try with the first example on the above wiki article…

For the 3rd method there are some requirements for X-SendFile to work… and you need to pass FULL path to the filename… again… check the above wiki article

thank you very much for your response mdomba… :D

I have tried the first example as you mentioned above, and yes it did work successfully.

Actually I have tried it before you told me to, but there was a mistake I made. I put my source code in view file, not in controller file…

That’s why I keep on getting error on download. Well, I’m sorry for being such a newbie… :(

And for the 3rd method, I have also tried to pass full path to the file name, as you told me. But still it gave me the same fail download result… :(

But for now I think it is enough to use the first method. :)

Thank you very much mdomba… :)

i use it. file download :D. but cann’t open. why? ???