Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 PHP强制更新图片缓存的示例代码

PHP强制更新图片缓存的示例代码

发布时间:2017-07-29   编辑:www.jquerycn.cn
分享一例php实现强制更新图片缓存的代码,用于开启了文件缓存或cdn加速等的站点上,是相当不错的,有需要的朋友参考下。

代码:
 

复制代码 代码示例:
<?php
/** 強制更新图片緩存
*   @param Array $files 待更新的图片
*   @param int $version 版本
*  @edit www.jbxue.com
*/ 
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 ob_start()缓存函数的用法详解
php 文件缓存数据类的代码分享
php立即刷新缓存输出的方法举例
php页面缓存的例子(减经cpu与mysql负担)
一个php缓存类与调用示例
php静态缓存提升网站访问速度的实现代码
php页面缓存ob系列函数的相关介绍
php禁止页面缓存输出的代码
php禁止页面缓存的代码
一个php的页面缓存类

您可能感兴趣的文章:
PHP强制更新图片缓存的示例代码
php绘图不显示图片怎么办
JS如何清除IE浏览器缓存
Nginx下php如何动态裁剪图片
php开发指南:缓存详解
PHPThumb图片处理实例解析
PHP输出缓冲及其应用的例子
清除IE缓存的方法汇总(asp asp.net php等)
php结合nginx实现动态裁剪图片
php图片处理类(生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转 )

关键词: 图片缓存   
[关闭]