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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

php2html php生成静态页函数

<?php /** ------------------------ Function: php2html($in_Url, $out_htmlFile, $out_logFile...

Thinkphp框架开发移动端接口(2)

接着上一篇介绍Thinkphp框架开发移动端接口(1),另外我们还可以通过ThinkPHP实现移动端访问自动切换主题模板,这样也可以做到移动端访问 ThinkPHP的模板主题机制,如果只...

php安全配置 如何配置使其更安全

另外,目前闹的轰轰烈烈的SQL Injection也是在PHP上有很多利用方式,所以要保证安全,PHP代码编写是一方面,PHP的配置更是非常关键。 我们php手手工安装的,php的默认...

centos 5.6 升级php到5.3的方法

centos 5.6 升级php到5.3的方法

升级很容易,先卸载 php 5.1.6 yum remove php* 然后安装 yum install php53* 就可以了...

如何取得中文字符串中出现次数最多的子串

直接上代码,子串的长度可自己设置(比如连续4个字符的或5个字符的)。复制代码 代码如下:$str ='我是中国人我是外国人我是韩国人我是美国人我是中国人我是英国人我是中国人我是外国人';...