Jump to content

Archived

This topic is now archived and is closed to further replies.

mmosel

File Download Error With Header Content-length! Need Help.

Recommended Posts

Hello all,

I am seeking some help with a problem that I just can't solve yet.

I have a download script where I use php to deliver a file. Here's the basic download script:

[code]
    $length = filesize($source);
                
     header('Pragma: private');
     header('Cache-control: private, must-revalidate');
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    
    
     header("Content-type: application/zip");
     header("Content-Transfer-Encoding: Binary");
     header("Accept-Ranges: bytes");
     header("Content - length: $length");
    
     header("Content-disposition: attachment; filename=filename.zip");
     readfile($source);
[/code]

This code works great, except that when I use the 'Content-length' header, the file downloads, and the downlaod queue shows the size of the file, but then the file doesn't open right. However, if I remove the content-length header altogether, then the file downloads just fine, and it opens fine. However, then the download queue doesn't know how large the file is!

I would like for the download queue to know how big the file is, so that the user can see how long it will take, etc. If anyone has any ideas or tips, I would love to know! Thanks.

NOTE: I had to alter the 'Content-length' line by adding spaces before and after the dash because the board wouldn't post the content. Strange.

Share this post


Link to post
Share on other sites
It may have something todo with the caching headers you are sending.

Also some browsers don't handle "Content-type: application/zip" very well, use "Content-Type: application/force-download" instead.

Try this for your caching headers,
FROM
[code]     header('Pragma: private');
     header('Cache-control: private, must-revalidate');
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  [/code]

TO
[code]    header("Pragma: public");
    header("Cache-Control: cache, must-revalidate"); [/code]

Also: Don't set an expiry date in the past, that means your 'file' has already expired (this method is often used to unset cookies). Either; Don't set it, or set it for the distant future.

HTH, Zac.

Share this post


Link to post
Share on other sites
Hi,

Thanks for all your suggestions! I tried them all, and unfortunately it still doesn't work right.

The thing is that it just doesn't seem to work right when I use the content-length header, but when I remove that, it works fine. When I do use the content-length header, it will save the file and it will show the right size while downloading, and it will save the file as the right size - but it won't open. This is with zip files. It's odd. I wish I could solve this.

Share this post


Link to post
Share on other sites
UPDATE: Well, I did some more testing, and it looks like my problem has to do with the fact that my code for the download is located inbetween an ob_start() and ob_end_flush() section. That's good and bad news. I can't really change the entire script and the output buffer. I need to find a way for it to work within the output buffer. It's still strange that the download works fine when I remove the content-length line. Any ideas out there?

Well, with some more testing, it turns out that if there is any output during the output buffer, then the header content-length is wrong and the file doesn't save properly on download. So, it's definitely an output buffer issue. Now if I could only figure out how to fix it!

Share this post


Link to post
Share on other sites
Hello,

In order to fix your problem, you will have to disable the compression which is not working with the Content-Length header (the browser is waiting more data than the stream received and it can corrupt the file).

Two easy solutions:
[list]
[*]Add this line at the top of your php file: [b]@ini_set('zlib.output_compression', 'Off');[/b]
[*]Add this line in the .htaccess file (create it if not present) in the folder where is your php file: [b]php_value zlib.output_compression Off[/b]
[/list]

Hope it helps!

Best regards from Switzerland!

André

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.