gpt4 book ai didi

php - file_get_contents 没有缓存?

转载 作者:行者123 更新时间:2023-12-02 11:25:09 25 4
gpt4 key购买 nike

我正在使用 file_get_contents() 从外部网站加载动态图像。

问题是远程网站上的图像已更新,但我的脚本仍在显示旧图像。我假设服务器将图像缓存在某处,但是如何强制服务器清除缓存并在使用 file_get_contents 获取文件时使用更新的图像?

在我的本地机器上,我必须按 CTRL+F5 强制刷新图像。

我也尝试在我的脚本中不添加缓存头,但它没有用:

    $image = imagecreatefromstring(file_get_contents($path));
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date dans le passé
header('Content-type: image/png');
imagepng($image);
exit();

最佳答案

您的问题是您正在使用外部资源来加载您的文件。一旦加载 - 向您的客户端发送一些标题是没有意义的。您的图像已加载(这是从外部资源缓存)。

但是,有一个简单的技巧可以解决问题。假设您正在使用类似 http://domain.com/path/to/image 的东西在您的 $path .然后就这样做:

$image = imagecreatefromstring(file_get_contents($path.'?'.mt_rand()));

-so 想法是向 GET 请求添加一些随机值并防止它被缓存。

关于php - file_get_contents 没有缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21754935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com