从互联网上复制了一些代码,试图通过另一个PHP代码(基本上尝试流式传输图像)对图像进行HTTP推送。
但页面似乎根本没有显示任何图像。以下是从http://www.howtocreate.co.uk/php/dnld.php?file=6&action=1
获取的完整代码function doServerPush($file,$type,$poll) {
if( !file_exists($file) ) {
//on first load, the file must exist, or it ends up sending a broken file
header('Content-type: text/html',true,404);
}
@set_time_limit(0); //PHP must not terminate this script
@ignore_user_abort(false); //do not ignore user aborts (may not be allowed, so status is checked in the loop)
@ini_set( 'zlib.output_compression', 'off' );
$poll *= 1000;
//need a unique boundary
//while it is possible to get a unique boundary for the current image data, it is possible that it will conflict
//with future image data - can't help this, just have to live with it - might as well use a static string
$separator = 'MTWJ_serverpush_boundary_';
for( $i = 0, $randChars = Array('A','B'); $i < 20; $i++ ) {
$separator .= $randChars[rand(0,1)];
}
header('Cache-Control: no-cache');
header('Pragma: no-cache');
//the header that makes server push work
header("Content-Type: multipart/x-mixed-replace;boundary=$separator",true);
$extrabreaks = '';
do {
//send one file starting with a multipart boundary
$filelen = filesize($file);
print "$extrabreaks--$separator\n";
if( !$extrabreaks ) { $extrabreaks = "\n\n"; } //Safari needs exactly one blank line, no extra linebreaks on the first one
print "Content-Type: $type\n";
print "Content-Length: $filelen\n\n";
readfile($file);
//Safari needs 200+ bytes between headers - IE needs 256+ per flush, and this will also take care of that, when IE gets server push
print str_pad('',200-$filelen); //whitespace here is ignored because of content-length
$lastupdsate = filemtime($file);
ob_flush();
flush();
$looptime = time();
do {
clearstatcache(); //need updated file info
usleep($poll);
if( time() - $looptime >= 10 ) {
//every 10 seconds, force it to re-evaluate if the user has disconnected (connection_aborted does not know until this happens)
//most use-cases will not need this protection because they will keep trying to send updates, but it is here just in case
$looptime = time();
print ' '; //whitespace is ignored at this specific point in the stream
ob_flush();
flush();
}
//poll the filesystem until the file changes, then send the update
//the file may not always exist for the instant it is being replaced on the filesystem
//nested loop and filemtime check inspired by http://web.they.org/software/php-push.php
} while( !connection_aborted() && ( !file_exists($file) || ( $lastupdsate == filemtime($file) ) ) );
} while( !connection_aborted() ); //if aborts are ignored, exit anyway to avoid endless threads
}
?>
调用代码的php文件如下:
require ("serverpush.php");
doServerPush("img.jpg",'image/jpeg',75);
,html如下:
<html>
<head>
<title>Streaming</title>
<style type="text/css" media="screen">
img
{
width: 200px;
height: 200px;
border: 2px solid #ABCDEF;
}
</style>
</head>
<body>
<h1>Image stream test</h1>
<img src="Stream.php" alt="Stream Image" />
</body>
</html>
任何帮助都将得到赞赏。试图在Firefox和Chrome中运行代码