代码:
test.php
<img src="test3.php?path=file/118812060.jpg">
<br>this is test!
test3.php
<?php
$path=$_GET["path"];
$cacheimgname=str_replace("/","_",$path);
$localimg="testimg/".$cacheimgname;
if ((file_exists($localimg)))
{
$httpurl=$localimg;
}
else
{
$httpurl="http://www.domain.com/".$path;
@copy($httpurl,$localimg);//缓存图片!
}
header("Location:$httpurl");
exit;
?>
实际地址为:testimg/file_118812060.jpg
本文介绍了一个使用PHP实现的简单图片缓存机制。当请求图片时,脚本首先检查本地是否存在缓存副本,如果不存在,则从指定URL下载并保存到本地。这种方式可以减少对外部资源的频繁访问,提高加载速度。
579

被折叠的 条评论
为什么被折叠?



