Yet another downloader problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Yet another downloader problem

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. you aren't varying the content-type in the code provided.
  2. add the content-length header to your headers.
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

Excellent, I'm sure I'm not the only one that this will help.

- dstefani
Post Reply