Re: Download adds ^M to beginning of file
| From: | Jochem Maas | Date: | Tue, 12 Dec 2006 15:27:27 +0000 |
| Subject: | Re: Download adds ^M to beginning of file | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-245757@lists.php.net to get a copy of this message | ||
lists@dwsasia.com wrote:
> Hi,
>
> Using the below code to force download works fine on most servers and
> with most
> browsers. However it does "crash" Firefox when I download a specific
> file, IE
> works like beauty. File content starts with:
>
> #!/sbin/_joor_perl_use Filter::decrypt......
>
> This is the code to force the download:
>
> 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-Transfer-Encoding: binary");
> header("Content-Length: $size");
> $file = fopen($completePath, 'r');
// no need to fopen/fclose if you decide to use readfile()...
>
> while(ob_get_level()) {
> ob_end_flush();
> }
// this should be:
while(ob_get_level()) ob_end_clean(); // discard all output!
>
> while(!feof($file)) {
> echo fread($file, 2048);
> }
>
> fclose($file);
// and this could probably be better written as:
@readfile($file); // read the whole file directly to the output buffer
>
> Where can I start? Could it be server config? Or could it have to do
> with flush?
at a guess: yes.
> If I remove the ob_end_flush() it does not crash Firefox.
>
> Best regards,
> Peter Lauri
>