PHP5 and file downloads woes

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
Fresshness
Forum Newbie
Posts: 3
Joined: Tue Jul 18, 2006 5:27 am

PHP5 and file downloads woes

Post by Fresshness »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a little PHP file which is used for downloading files off the site. This little PHP file basicly just sets the correct headers and a readline statement like so:

Code: Select all

header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: private",false);
   header("Content-Type: application/octet-stream");
   header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".@filesize($filename));
   @readfile("$filename");

When this PHP file is used on a PHP4 enabled Apache server, all goes OK.
But, when it's used on a PHP5 enabled server the file that is downloaded is corrupted.

The file is 'corrupted' in the following way:
The file is 2 bytes bigger than originally. These 2 bytes in question are 0D 0A at the very beginning of the file (first 2 bytes thusly). I think that's a newline (or "\n").

I cannot change this 'corrupt' behaviour no matter what I try...


Can anyone offer my some insight? It's sorely needed

Thanks!


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Have you got any output anywhere on the page (including what is outside the '<?php' and '?>') ?

remove the error supression operators.. (@) and see if any errors are generated.
eXpertPHP
Forum Newbie
Posts: 5
Joined: Sun Mar 12, 2006 9:11 am
Contact:

Post by eXpertPHP »

The file is 2 bytes bigger than originally. These 2 bytes in question are 0D 0A at the very beginning of the file

Code: Select all

if(file_exists($filename) && is_readable($filename)){
 // put header settings, then instead of reafile() put this:
 $data = file_get_contents($filename);
 echo substr($data, 2);
}
Fresshness
Forum Newbie
Posts: 3
Joined: Tue Jul 18, 2006 5:27 am

Post by Fresshness »

Thanks Pimptastic, for making my post human readable :wink: I'll try to do better in future.

Now, for the problem at hand; in reply to the kind sir Jenk:
remove the error supression operators.. (@) and see if any errors are generated.
I've tried this and the same thing is happening (with no error).

The little workaround function from eXpertPHP didnt' do the trick either: When I tried this, the first 2 (crucial) bytes from the file itself is left out and in it's place is now a '\n'.

I really think this is done somewhere internal in PHP5, because I've tried numerous varations to the @readline($filename) statement, all yielding the same result.

I'm not posting my php.ini deliberatly, because I think it'l clog up the thread too fast. Ofcourse, If anyone is interested to see it (or my httpd.conf), I'll be happy to post it.

Thanks so far!
Fresshness
Forum Newbie
Posts: 3
Joined: Tue Jul 18, 2006 5:27 am

Post by Fresshness »

Where can I officially post a bug?

I've searched all around and I'm unable to find it :? Can anyone help or point me in the right direction ?

PS: I already know google :lol:
Post Reply