file_get_contents没有缓存? [英] file_get_contents no caching?

查看:547
本文介绍了file_get_contents没有缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using file_get_contents() to load a dynamic image from an external website.

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

The problem is that the image has been updated on the remote website but my script is still displaying the old image. I assume the server cache the image somewhere but how can i force the server to clear the cache and use the updated image when getting the file with file_get_contents ?

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

On my local machine, i had to do CTRL+F5 to force refresh on the image.

我还尝试在我的脚本中不添加缓存头,但它不起作用:

I also tryed to add no cache header to my script, but it didn't work:

    $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();


推荐答案

你的问题是你正在使用用于加载文件的外部资源。加载后 - 将一些标题发送到您的客户端是没有意义的。您的图片已经已加载(这是来自外部资源的缓存)。

Your problem is that you're using external resource to load your file. Once it was loaded - there's no sense to send some headers to your client. Your image already been loaded (and that was cache from external resource).

但是,解决问题的方法很简单。假设您在 $ path http://domain.com/path/to/image 的内容>。然后就做:

However, there's easy trick to resolve an issue. Let's suppose you're using something like http://domain.com/path/to/image in your $path. Then just do:

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

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

-so idea is to add some random value to GET-request and prevent it from being cached.

这篇关于file_get_contents没有缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆