Forum Moderators: phranque

Message Too Old, No Replies

Apache upgrade leads to streming issue

My host upgraded from Apache 1.3 to 2.2.3, breaking some simple stream code

         

citpes

10:57 am on Apr 8, 2010 (gmt 0)

10+ Year Member



Hi Guys

I am pulling my hair out on this one. I have some really simple code, that outputs images with php. This code works on my localhost (and about 10 other friends I have asked to check). It even worked with my online host until they upgraded the version of Apache from 1.3 (I think) to 2.2.3.
The weird thing is it only seems to fail on some images, other it works fine with.

Some searching brought up the possibility that EnableSendfile and EnableMMap need to be set to off to fix the problem.

I requested the change from my host, who say they have made it, but the streaming code still doesnt work on some images.

Firstly, is there anyway I can check they have turned the options off (phpinfo doesn't seem to show it).
Secondly, is there anything else that anyone thinks might be the problem.

Here is the code:

<?php
function streamGif($file) {
if (is_file($file)) {
while(ob_get_level()) {ob_end_clean();}
header('Content-Type: image/gif'); //tell the browser its a gif
header('Content-Length: '.filesize($file)); //tell the browser how big
header('Last-Modified: '.date('D, d M Y H:i:s', filemtime($file)).' GMT'); //last modified, so it can be cached correctly
echo file_get_contents($file); //output the binary of the file
exit;
} else {
print($file.' is not file?');
}
}

streamGif('controlpanel_toprepeat.gif');
?>

Obviously that image is one of the ones breaking. If I use certain different images, they work fine.

Thanks in advance!

jdMorgan

12:12 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may do better asking in the PHP forum on this one. But either way, some basic info is missing:

Who wrote this PHP code? -- Did you write it, or is it a 'standard' and therefore previously-tested routine?

-- and, most important -- How, specifically, does the function fail? What do you see in your browser when it fails? Anything useful in the server error logs?

Also, are there any noticeable commonalities between all images that 'work' or between all images that 'fail'? Are there any noticeable differences between these 'work' and 'fail' groups?

Jim

citpes

12:28 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Hi Jim
Thanks for the quick response. This specific example I wrote to demonstrate the problem.
It is however tested successfully on over a dozen different setups, while I have been trying to isolate the issue. That's why I posted it under the Apache forum, because I think it is a setup issue.

Just looking now, the failed images do mostly seem to be GIF89a, but I have just tested with a random GIF, and all seems fine with it. (I have resaved the image I know is not working, to be sure it is not corrupt).

So no, I guess I can't see any obvious grouping of the success and failed to load images.

The image I am testing with that is not working can be downloaded from example.com/controlpanel_topleft.gif if you want to check it.
I also have the php running fine on that server.

[edited by: jdMorgan at 1:52 pm (utc) on Apr 8, 2010]
[edit reason] No URLs, please. See Terms of Service and forum charter. [/edit]

citpes

1:23 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Oh, and one more thing I remembered. If I comment out the 'Content-Length' header, it works fine...

jdMorgan

1:59 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Image file size, good versus bad?
Image location -- different (sub)directory paths?

Note also (as an unrelated side issue, but mentioned because you said "mostly seem to be GIF89a") that the MIME-type is hard-coded in your script... Do you also use it to serve JPEG or PNG files?

Your latest post indicates a problem with generating or detecting the filesize. That's where I'd look first.

Be sure to delete your browser cache after making any server-side changes -- This will prevent your browser giving you stale images and server response codes.

Jim

citpes

2:00 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



I am having a look at the headers when the image fails to load, and I see the Content-Encoding is gzipped.
Could that mean the size I put in the Content-Length is wrong, and that's why it works when I remove the header?

citpes

2:18 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Hey
I have just noticed that there is gz compression on the output now. Obviously I am sending the content-length header from the filesize() which is uncompressed.
But with small images (15x9 in this case), the gzipped output could be bigger than the content-length header.

Could that mean the browser stops reading the output too soon an doesn't load the image? Sounds viable to me, anyone know enough to comment?

@jdm, the script I posted was just to illustrate the problem. The actual script that is on my site checks mime types etc. I isolated it to a file type just for simplicity on the forum :)

jdMorgan

3:09 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is possible that the gzip configuration is set to ignore small filesizes, and therefore, the Content-Length is correct for small files but incorrect for larger files. That's one reason I suggested investigating the commonalities and differences in the pass-fail cases.

I'm not sure if you can detect whether the images have been compressed or not from within PHP, but if not, perhaps you can find the current filesize threshold(s) for compression and can suppress the Content-Length header based on that information -- with BIG UGLY ALL-CAPS comments in both the script and the gzip config file to warn everyone (including yourself) that both must be changed if either is changed. :)

Jim

citpes

7:13 am on Apr 9, 2010 (gmt 0)

10+ Year Member



Looks like a code change is in order, to detect compression and serve the content-length header appropriately.
Unfortunately, that's exactly what I was wanting to avoid, as I have dozens of site spread across a bunch of servers with the same hosting company.

Oh well, might as well just get to it.

Thanks for the input!