image file truncated

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
leks
Forum Newbie
Posts: 2
Joined: Thu Jul 20, 2006 10:21 am

image file truncated

Post 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]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Take a look at the modes that you are using in fopen()
leks
Forum Newbie
Posts: 2
Joined: Thu Jul 20, 2006 10:21 am

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply