php PHP强制刷新图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16526363/
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
PHP force refresh image
提问by DorianHuxley
I'm creating an input form that allows users to upload an image. Once it has been submitted the user is returned to the form with a conformation bit at the top.
我正在创建一个允许用户上传图像的输入表单。提交后,用户将返回到顶部带有构造位的表单。
Any image that has been submitted is then previewed via a query to a MySQL database that stores the URL of the image. However, if the user uploads a photo, it is previewed, and then they decide to change that photo and resubmit the form, when it takes you back to the form the new photo doesn't show unless you manually press refresh on the browser.
然后通过对存储图像 URL 的 MySQL 数据库的查询来预览任何已提交的图像。但是,如果用户上传照片,它会被预览,然后他们决定更改该照片并重新提交表单,当它带您返回表单时,除非您在浏览器上手动按刷新,否则新照片不会显示。
Is there any way of forcing a refresh/cache deletion or refresh for that specific image rather than the whole page?
有没有办法强制刷新/缓存删除或刷新该特定图像而不是整个页面?
Thanks
谢谢
回答by Reactgular
Add the modified date to the end of the image as a query.
将修改日期作为查询添加到图像的末尾。
<img src="/images/photo.png?=1239293"/>
In PHP
在 PHP 中
<img src="/images/photo.png?=<?php echo filemtime($filename)?>"/>
回答by Arvy
In PHP you can send a random number or the current timestamp:
在 PHP 中,您可以发送一个随机数或当前时间戳:
<img src="image.jpg?<?=Date('U')?>">
or
或者
<img src="image.jpg?<?=rand(1,32000)?>">
回答by Christian Stewart
The trick here is to tell the browser to not cache the content. You can do this by putting these statements before ANYTHING else (no whitespace or anything!)
这里的技巧是告诉浏览器不要缓存内容。您可以通过将这些语句放在其他任何东西之前(没有空格或任何东西!)
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Depending on how you're loading the image, you can also do a javascript refresh of the image using jQuery.
根据您加载图像的方式,您还可以使用 jQuery 对图像进行 javascript 刷新。
回答by noamik
You could replace the current image by a new one using JavaScript:
<div id="imageHolder">this is where the image will show</div>
<script type="text/javascript">
function myfunction(){
document.getElementById("imageHolder").innerHTML="<div id=\"imageHolder\"><img src='imagefolder/imagename.jpg' alt=''/></div>";
}</script>
You need to trigger the function myfunction after completing the upload of course ...
当然需要在上传完成后触发功能myfunction...
回答by Bas
This worked great for me:
这对我很有用:
image.jpg?=<?php echo rand() . "\n";?>