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

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

相关文章

PHP递归实现文件夹的复制、删除、查看大小操作示例

本文实例讲述了PHP递归实现文件夹的复制、删除、查看大小操作。分享给大家供大家参考,具体如下: PHP虽然提供了 filesize、copy、unlink 等文件操作的函数,但是没有提供...

解析php中eclipse 用空格替换 tab键

本人用eclipse进行PHP项目开发,根据公司开发规范,要用4个空格替换掉tab键,在网上搜了好多,都不太全面,现在将详细的配置陈列如下:首先进入首选项:窗口(windows)->...

php的debug相关函数用法示例

本文实例讲述了php的debug相关函数用法。分享给大家供大家参考,具体如下: loginfo函数: function loginfo($format) { $args = fu...

PHP中call_user_func_array回调函数的用法示例

call_user_func_array call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 mixed call_user_func_a...

php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)

那么如果考虑突破防盗链的措施,就需要考虑在 HTTP_REFERER 上面做手脚了。PHP 脚本中对应的变量是 $_SERVER['HTTP_REFERER'] ,它存储了 HTTP_R...