PHP和htaccess,如何仅禁用一个目录中的图像缓存

3

我的根目录下有一个htaccess文件,其中包含以下缓存指令:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

现在我注意到,当我使用php/ajax上传图片时,第一次预览显示正确的图片,然而如果我重新上传/覆盖图片,则预览不会更改,这可能是由于htaccess缓存指令造成的。
是否可以只在目录/upload/中删除缓存,或者更好地,在upload/preview php脚本中禁用缓存,以便始终正确显示预览?
1个回答

4
在上传文件夹中添加另一个.htaccess文件,其内容为:
ExpiresActive Off

对于0秒的图像,可以使用ExpiresByType。

或者在预览脚本中为您的图像标签添加缓存破坏器:

src="/uploads/image.jpg?<?php print time(); ?>"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接