分享php如何现实强制更新图片缓存的方法(附代码)

分类:PHP_Python| 发布:佚名| 查看:354 | 发表时间:2015/6/28

php强制更新图片缓存的具体实现方法如下:

代码如下:
01/** 强制更新图片缓存
02 *   @param Array $files 要更新的图片
03 *   @param int $version 版本
04 */ 
05 function force_reload_file($files=array(), $version=0){ 
06     $html = ''
07     if(!isset($_COOKIE['force_reload_page_'.$version])){ // 判断是否已更新过 
08         setcookie('force_reload_page_'.$version, true, time()+2592000); 
09         $html .= '<script type="text/javascript">'."\r\n"
10         $html .= 'window.onload = function(){'."\r\n"
11         $html .= 'setTimeout(function(){window.location.reload(true); },1000);'."\r\n"
12         $html .= '}'."\r\n"
13         $html .= '</script>'
14         echo $html
15         exit(); 
16     }else// 读取图片一次,针对chrome优化 
17         if($files){ 
18             $html .= '<script type="text/javascript">'."\r\n"
19             $html .= "<!--\r\n"
20             for($i=0,$max=count($files); $i<$max; $i++){ 
21                 $html .= 'var force_reload_file_'.$i.' =new Image()'."\r\n"
22                 $html .= 'force_reload_file_'.$i.'.src="'.$files[$i].'"'."\r\n"
23            
24             $html .= "-->\r\n"
25             $html .= '</script>'
26        
27    
28     return $html
29 
30    
31 // 调用方法 
32 $files = array
33     'images/1.jpg'
34     'images/2.jpg'
35     'images/3.jpg'
36     'images/4.jpg' 
37 ); 
38 $html = force_reload_file($files, 1); 
39 echo $html;
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/php-python/2050.html