PHP使用带宽限制和缓存提供图像?
是指向文件的带宽限制PHP脚本示例的链接。我看到了一个我可以做的改进,这与我以后会做的无关,但除此之外。。。如何使用此脚本生成另一个脚本,该脚本返回带宽受限的图像,但永久缓存图像?图像永远不会更改。我认为您所要求的只是修改脚本以处理图像并缓存内容。这应该可以做到,在我可能有任何小错误之前:PHP使用带宽限制和缓存提供图像?,php,image,caching,bandwidth,bandwidth-throttling,Php,Image,Caching,Bandwidth,Bandwidth Throttling,是指向文件的带宽限制PHP脚本示例的链接。我看到了一个我可以做的改进,这与我以后会做的无关,但除此之外。。。如何使用此脚本生成另一个脚本,该脚本返回带宽受限的图像,但永久缓存图像?图像永远不会更改。我认为您所要求的只是修改脚本以处理图像并缓存内容。这应该可以做到,在我可能有任何小错误之前: <?php $file = "yourimage.jpg"; // file to be send to the client $speed = 8.5; // 8,5 kb/s download r
<?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);
}