Page 1 of 1

Yet another downloader problem

Posted: Thu Apr 07, 2005 5:58 am
by dstefani
Hello and thanks...

I have a download script that has worked fine for some time, but now I am having problems. It will download pdf's fine, but when I try to download a csv file, I get zero file size, yet the actual file on the server is full and right.

After doing a bit of searching here, I tried varying the content-type for the csv, but it doesn't help.

I've been using Firefox 1.0 Mac as my testing browser. This is being used on an intranet and that's their browser.

Here is the code:

Code: Select all

<?php

if($_REQUEST['d'] == 1 ) { // a csv file
    $file=$_REQUEST['file'];
    $root = 'path/to/my/file/';
    $file_to_download = $root.$file;
    $type = 'text/csv';
}

else { // anything else
    $root = 'path/to/my/file/';
    $file=$_REQUEST['file'];
    $file_to_download = $root.$file;
    $type = 'application/octet-stream';
}


header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/octet-stream");
header ('Content-Disposition: attachment; filename='.$file);

header( "Content-Description: File Transfer");
@readfile($file_to_download);
?>
Thanks,

- dstefani

Posted: Thu Apr 07, 2005 6:24 am
by feyd
  1. you aren't varying the content-type in the code provided.
  2. add the content-length header to your headers.

Posted: Thu Apr 07, 2005 6:51 am
by dstefani
Thanks for pointing out the missing $type, your suggestion of content-length was what helped me.

I wasn't sure what to set the length at so I did a filesize() on the $file_to_download var and got back a php can't stat error. Huh?

Then I noticed I had the file name in the path ($root) and then I appended it again to the path. DUH.

So now all is good.

If I am not using the content-length properly, can you direct me to some info on it?

Thanks!

- dstefani

Posted: Thu Apr 07, 2005 8:32 am
by timvw

Posted: Thu Apr 07, 2005 8:42 am
by dstefani
Excellent, I'm sure I'm not the only one that this will help.

- dstefani