php怎么强制更新图片缓存

本篇内容主要讲解“php怎么强制更新图片缓存”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php怎么强制更新图片缓存”吧!

复制代码 代码如下:

/** 強制更新圖片緩存
*   @param Array $files 要更新的圖片
*   @param int $version 版本
*/ 
function force_reload_file($files=array(), $version=0){ 
    $html = ''; 
    if(!isset($_COOKIE['force_reload_page_'.$version])){ // 判斷是否已更新過 
        setcookie('force_reload_page_'.$version, true, time()+2592000); 
        $html .= '<script type="text/javascript">'."\r\n"; 
        $html .= 'window.onload = function(){'."\r\n"; 
        $html .= 'setTimeout(function(){window.location.reload(true); },1000);'."\r\n"; 
        $html .= '}'."\r\n"; 
        $html .= '</script>'; 
        echo $html; 
        exit(); 
    }else{  // 讀取圖片一次,針對chrome優化 
        if($files){ 
            $html .= '<script type="text/javascript">'."\r\n"; 
            $html .= "<!--\r\n"; 
            for($i=0,$max=count($files); $i<$max; $i++){ 
                $html .= 'var force_reload_file_'.$i.' =new Image()'."\r\n"; 
                $html .= 'force_reload_file_'.$i.'.src="'.$files[$i].'"'."\r\n"; 
            } 
            $html .= "-->\r\n"; 
            $html .= '</script>'; 
        } 
    } 
    return $html; 

 
// 调用方法 
$files = array( 
    'images/1.jpg', 
    'images/2.jpg', 
    'images/3.jpg', 
    'images/4.jpg' 
); 
$html = force_reload_file($files, 1); 
echo $html;

到此,相信大家对“php怎么强制更新图片缓存”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!