So far, I've tested a call to fopen followed by a call to fpassthru
on a remote (http://...) JPEG file with PHP v4.4.7 in the "OSU Web Server"
running under OpenVMS V8.3 on an Alpha, and with PHP v5.3.4 in Apache v2.2.17
running under Mac OS X v10.6.8. The JPEG file is served by a Webcam. The
documentation (on
php.net) for "http://" says the stream opened by fopen only
provides access to the body of the response. Why is it, then, that when I
call on fpassthru for the (JPEG URL) stream, I see the headers included in the
data returned?
The code looks basically like this:
$image = fopen ( "http://...jpg", "r" );
foreach ( $http_response_header as $header )
{
echo "{$header}\r\n";
}
echo "\r\n";
fpassthru ( $image );
fclose ( $image );
With the above, the headers are clearly visible at the beginning of the data,
rendering a lot of meaningless code in the browser window, rather than an
actual image. One work-around I've developed is...
$image = fopen ( "http://...jpg", "r" );
$inHeaders = 1;
while ( $inHeaders && ! feof ( $image ) )
{
$line = fgets ( $image );
echo $line;
$inHeaders = ( $line != "\r\n" );
}
if ( ! feof ( $image ) )
fpassthru ( $image );
fclose ( $image );
Can anyone explain why I'm seeing behavior counter to the documentation with
"fopen ( 'http://...' )" and "fpassthru (...)"? (FWIW, the purpose of the
code is to "proxy" a Webcam image that isn't directly available, so that as
far as the browser is concerned, the image is served by the web server
hosting this PHP code.)
Thanks,
Mike