Page 1 of 1

image file truncated

Posted: Thu Jul 20, 2006 10:23 am
by leks
hawleyjr | 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]


Hi,

I am having some problems sending image files to the browser from php. I am opening a file from the folder and sending it to the browser in the following way:

Code: Select all

$tag = fopen('myfile.jpg', 'rb');
if ($tag) {
header('Content-Type: image/jpeg');
fpassthru($tag);
}

This code only returns the first four bytes of the image e.g. ÿØÿà .
After opening the image with a hex editor, I noticed that the next character, after the first four bytes is null. So it seems that the null character is wrongly interpreted by either php or web server as end of file, thereby truncating the image file.


The example works on my home machine, but for some reason fails on the hosting server. I am guessing it is probably a bug in web server or php version (4.3.11) they are running on the server (I'm trying to get them to look into it ).


any ideas/suggestions would be useful
thanks
Reks


hawleyjr | 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]

Posted: Thu Jul 20, 2006 10:26 am
by hawleyjr
Take a look at the modes that you are using in fopen()

Posted: Thu Jul 20, 2006 10:56 am
by leks
hawleyjr wrote:Take a look at the modes that you are using in fopen()
I don't think that is the problem, r is for reading a file and b is just an additional parameter which specifies that the file being opened is binary:

According to php.net

"If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters." - http://www.php.net/fopen

Posted: Thu Jul 20, 2006 11:17 am
by onion2k
For the record, fpassthru() is an amazingly slow function. Bizarrely slow. It's about 1/2 the speed of doing while(!feof($file)) echo fread($file, 4096); .. weirdness.