以下是文件的带宽限制 PHP 脚本示例的链接。我看到了我可以做出的改进,这与我稍后会自己做的无关,但除此之外......你怎么能使用这个脚本来制作另一个脚本,它返回一个限制带宽的图像,但永远缓存图像?形象永远不会改变。
user263078
问问题
62 次
1 回答
1
我认为您所要求的只是修改该脚本以使用图像并缓存内容。应该这样做,等待我可能遇到的任何小错误:
<?php
$file = "yourimage.jpg"; // file to be send to the client
$speed = 8.5; // 8,5 kb/s download rate limit
// if $file is coming from get, I would use this to prevent against a nullbyte attack:
$file = str_replace(chr(0), '', $file);
if (file_exists($file) && is_file($file)) {
header("Expires: ".gmdate('D, d M Y H:i:s', time()+3600*24*3000).'GMT'); // expires in 3000 days.
header("Pragma: cache");
header("Content-Type: image/jpeg"); // needs to be changed for the file type.
header("Content-Length: ".filesize($file));
header("Cache-Control: max-age=" . 3600*24*3000);
flush();
$fd = fopen($file, "r");
while(!feof($fd)) {
echo fread($fd, round($speed*1024));
flush();
sleep(1);
}
fclose ($fd);
}
于 2012-02-16T20:20:52.630 回答