Content-Length header not working!

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
lwi
Forum Newbie
Posts: 3
Joined: Fri Aug 06, 2004 9:53 am

Content-Length header not working!

Post by lwi »

Hi,

I have a small problem with a download script I have made. Here is the code:

Code: Select all

(snipped authentication / anti-leech code)
$size = filesize($path);
header("Pragma: ");
header("Cache-Control: ");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="$fileName"");
header("Content-Length: ".$size);

@readfile($path);
die();
The file is downloaded properly, without errors or corruption, but the size of the file is always unknown to the browser, even though I specify it with the Content-Length header. I really don't know why.

Can anybody help?

Thanks in advance,

Lwi
Last edited by lwi on Fri Aug 06, 2004 2:12 pm, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Does the path variable represent the path and the file or just the directory path of your file?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I just looked up some code on how I did it. Try this:

Code: Select all

<?php
header ('Content-Length: '.$size. "bytes");
?>
lwi
Forum Newbie
Posts: 3
Joined: Fri Aug 06, 2004 9:53 am

Post by lwi »

the $path variable represents both the path and the file name.

In your example there is no space between the size and the word 'bytes'... i assum it's a slight mistake. Either way, I tried both ways:

Code: Select all

<?php
header ('Content-Length: '.$size. "bytes");
?>
and

Code: Select all

<?php
header ('Content-Length: '.$size. " bytes");  // with space before 'bytes'.
?>
None of it seems to work. :(
lwi
Forum Newbie
Posts: 3
Joined: Fri Aug 06, 2004 9:53 am

Post by lwi »

Ok... I downloaded this very useful little gizmo: http://www.blunck.info/iehttpheaders.html. It allows you to view the HTTP headers sent to the browser.

It showed me that the content lenght header is actually sent to the browser, which I find odd.

Here are the headers:
HTTP/1.1 200 OK
Date: Fri, 06 Aug 2004 19:26:10 GMT
Server: Apache/2.0.48 (Unix) mod_ssl/2.0.48 OpenSSL/0.9.7c DAV/2 PHP/4.3.4
X-Powered-By: PHP/4.3.4
Set-Cookie: CerberusPublicGUI=33542c26f030fba2cc41cb22333283aa; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control:
Pragma:
Content-Length: 5310955
Content-Disposition: attachment; filename=AlgolithAE60Setup1.9.zip
Connection: close
Vary: User-Agent
Content-Type: application/octet-stream
But the file size is still unknown (screen shot from Opera):
Image

I'm even more puzzled now..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

May be a non-issue, but when I send the content length header, "length" is all lowercase, not "Length".
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Have you tried in different browsers?
Post Reply