php 在php中重新上传具有相同文件名的图像时如何清除浏览器缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3137934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear browser cache when re-uploading image with same filename, in php?
提问by niallo
I have a listing of items, that also contains an image thumbnail. Sometimes I need to change the thumbnail image, but keep the filename the same. Is there a way to force the browser to download the newly uploaded image, instead of grabbing one from the browser cache?
我有一个项目列表,其中还包含一个图像缩略图。有时我需要更改缩略图图像,但保持文件名不变。有没有办法强制浏览器下载新上传的图片,而不是从浏览器缓存中抓取?
This is very pertinent when people update their avatars, which causes confusion for some users, who keep seeing their old avatar, until they clear the browser cache, or restart the browser.
当人们更新他们的头像时,这非常相关,这会导致一些用户感到困惑,他们一直看到他们的旧头像,直到他们清除浏览器缓存或重新启动浏览器。
回答by niallo
Append a unique number to the end of the image src
将唯一编号附加到图像 src 的末尾
ie
IE
<?php
echo "<img src=../thumbnail.jpg?" . time() . ">";
do this only on the form for the updating avatar
仅在更新头像的表单上执行此操作
works for me
为我工作
btw, this works well for forcing updates of other static files
顺便说一句,这适用于强制更新其他静态文件
i.e. html for css files is
即 css 文件的 html 是
<link rel="Stylesheet" href="../css/cssfile.css?<?= md5_file('cssfile.css'); ?>" />
回答by Leo
You can use the modification time of file:
您可以使用文件的修改时间:
<?
$filename = 'avatar.jpg';
$filemtime = filemtime($filename);
echo "<img src='".$filename."?".$filemtime."'>";
//result: <img src='avatar.jpg?1269538749'>
?>
when the file is modified will clear the client cache
当文件被修改时会清除客户端缓存
回答by TomWilsonFL
回答by Emil Vikstr?m
When sending out the image, make sure not to send any Expiresheader. Also consider sending this cache header:
发送图像时,请确保不要发送任何Expires标题。还可以考虑发送这个缓存头:
Cache-Control: must-revalidate
This will make the browser ask your server for the image every time. Watch for an If-Modified-Since header in the request; if the image is not modified, answer with an 304 HTTP code.
这将使浏览器每次都向您的服务器询问图像。注意请求中的 If-Modified-Since 标头;如果图像没有被修改,回答一个 304 HTTP 代码。
This whole procedure (must-revalidate, If-Modified-Since, 304 answer) will make it possible for the browser to cache the image content but at the same time ask your server if the file has changed.
整个过程(必须重新验证,If-Modified-Since,304 答案)将使浏览器可以缓存图像内容,但同时询问您的服务器文件是否已更改。
Another, maybe simpler, solution is to only set an Expiresheader a short time in the future, for example ten minutes, and inform the user of the ten minute delay. This will speed up most page loads since there's no need to do any HTTP request for the image at all.
另一种可能更简单的解决方案是只在未来短时间内设置Expires标题,例如十分钟,并通知用户十分钟的延迟。这将加快大多数页面加载,因为根本不需要对图像进行任何 HTTP 请求。
回答by vitsoft
Cached images are also invalidated on manual page refresh (pressing F5 in browser). This can be simulated with Javascript method location.reload() being fired on body load. To avoid perpetual reloading, this may take place only once. When the user uploads a new avatar, the ReloadPending request is set persistently in his/her session and it is reset when it's been answered.
缓存的图像也会在手动页面刷新时失效(在浏览器中按 F5)。这可以通过 Javascript 方法 location.reload() 在身体负载上被触发来模拟。为了避免永久重新加载,这可能只发生一次。当用户上传新头像时,ReloadPending 请求会在他/她的会话中永久设置,并在被回答时重置。
<?php
if ($session['ReloadPending'])
{ $session['ReloadPending']=false;
echo "<body onload='location.reload(true);'>";
} else echo "<body>";
?>
Reloading the whole page after avatar upload will cause the page flicker but this happens only once, which, I believe, is acceptable.
上传头像后重新加载整个页面会导致页面闪烁,但这只会发生一次,我相信这是可以接受的。
回答by Pooja Gupta
Please use these rules in your .htaccessfile.
I used these rules and solved my problem related to image re-uploading with the same file name.
请在您的.htaccess文件中使用这些规则。我使用了这些规则并解决了与使用相同文件名重新上传图像相关的问题。
# BEGIN Caching
<ifModule mod_headers.c>
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|ttf|otf|woff|woff2|eot|svg)$">
Header set Cache-Control "max-age=0, public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>
# END Caching
回答by Asesha George
You can add last modified date to you image or simply add date and time at the end of you image URL
您可以将上次修改日期添加到图像中,也可以在图像末尾添加日期和时间 URL
for Example
例如
$today=date('Y-m-d H:i');
<img class="img-circle pro_pic" src="https://govcard.net/yourimage.jpg?lm=<?php echo $today?>" alt="">
after .jpg add date and time like this. hope this will help you.
在 .jpg 之后添加这样的日期和时间。希望这会帮助你。
回答by Hatch
I found out that one of the best ways to do this is by having the same URL for getting and posting an image (REST).
我发现最好的方法之一是使用相同的 URL 来获取和发布图像 (REST)。
POST /my/image (Cache-control: no-cache, must-revalidate)
GET /my/image (Cache-control: must-revalidate)
POST /my/image(缓存控制:无缓存,必须重新验证)
GET /my/image(缓存控制:必须重新验证)

