首页 > 编程 > PHP > 正文

php强制更新图片缓存的方法

2019-11-21 23:17:03
字体:
来源:转载
供稿:网友

本文实例讲述了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程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表