巨型网址优化技巧,多图片上传程序代码

静态能源文件自动减削并替换到压缩版本(大型网址优化本领)

2015/11/26 · HTML5 ·
静态能源

原版的书文出处: Kelly   

那二回,作者计算和享用生龙活虎项大型网址优化技艺,那正是在档期的顺序中自行减少静态财富文件(css、js),并让网址自动加载压缩后的财富文件。当然,那项才具在雅虎35条前端优化建议里也是有记载,但它那只是给出三个反驳的方案而已,况兼选择的是外表压缩工具去减弱,而在本身的品类中,是一直通过和谐的主次自动化去收缩全数css、js文件,然后让页面一贯加载所缩小后的财富,接下去间接步向正题。

本次实验应用的是PHP脚本语言,版本是PHP5.6,是在LINUX下搭建的意况(网络搭建无论是搭建LAMP还是LNMP的科目都五光十色颠倒错乱,下一次小编会总括和享用什么在LINUX下搭建服务器情状的博文,何况搭建的条件必需叁回性搭建变成功的)。所选取的框架是CI框架,所接收的沙盘模拟经营是斯马特y模板引擎。当然了,那一个只是本人所使用的景况而已,尽管您是PHP开垦者,假若你要测量试验下此番试验,那么,小编建议您的PHP版本接纳5.4上述,至于框架用哪些都以能够的。而借让你不是PHP开采者(你是JSP大概是ASP开荒者只怕是此外开辟者),那么你掌握好那黄金时代思路后,完全能够在大团结深谙的言语里开展实验测验。

一、原理图

首先自身画一张思路图,便于大家先知道。

先是是能源收缩原理图:

澳门葡京 1

任何时候是财富文件替换的规则图:

澳门葡京 2

假诺大家认真精晓何况看懂这两张原理图的话,基本上也就调节了自身所享受的思绪。假诺依旧不可能领略的话,接下去作者会结合代码,对以上原理图的每一步进行详细解说。

二、思路详细深入分析

1.先是是调用该滑坡的点子,你能够把该方法放在网址所要加载的公共类的地点,举例每一回访谈网址都会调用该滑坡方法举办压缩。当然,那个只是在付出条件才会每回都调用,假使是线上的遭受,在你的网址发三次新本子的时候,调用一次用来生成压缩版的静态能源就能够了。

class MY_Controller extends CI_Controller { public function
__construct() { parent::__construct(卡塔尔国; //压缩jscss财富文件
$this->compressResHandle(卡塔尔(英语:State of Qatar); } /** * 压缩js、css能源文件(优化) *
@return [type] [description] */ private function
compressResHandle(卡塔尔 { $this->load->library(‘ResMinifier’卡塔尔(英语:State of Qatar);
//压缩钦赐文件夹下的财富文件 $this->resminifier->compressRes(卡塔尔国; }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
 
        //压缩jscss资源文件
        $this->compressResHandle();
    }
    /**
     * 压缩js、css资源文件(优化)
     * @return [type] [description]
     */
    private function compressResHandle() {
        $this->load->library(‘ResMinifier’);
        //压缩指定文件夹下的资源文件
        $this->resminifier->compressRes();
    }
}

2.接着就调用了 ResMinifier类里的 compressRes方法。在这边作者先附上
Res迷你fier那几个类的代码,然后方便一步步开展分析教学

PHP

<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
/** * 资源压缩类 */ class ResMinifier { /** 必要减小的能源目录*/
public $compressResDir = [‘css’, ‘js’]; /**
忽视压缩的必由之路,比方此处是js/icon开首的路子忽视压缩*/ public
$compressResIngorePrefix = [‘js/icon’]; /** 财富根目录*/ public
$resRootDir; /** 财富版本文件路线*/ private $resStatePath; public
function __construct(卡塔尔(قطر‎ { $this->resRootDir = WEBROOT . ‘www/’;
$this->resStatePath = WEBROOT . ‘www/resState.php’; } public function
compressRes(卡塔尔(英语:State of Qatar) { //获取寄放版本的能源文件 $resState =
$this->getResState(卡塔尔; $count = 0; //带头遍历需求收缩的财富目录
foreach ($this->compressResDir as $resDir卡塔尔(英语:State of Qatar) { foreach (new
RecursiveIteratorIterator(new
RecursiveDirectoryIterator($this->resRootDir . $resDir ,
FilesystemIterator::SKIP_DOTS)卡塔尔国 as $file卡塔尔国 { //获取该能源文件的相对路径$filePath = str_replace(‘\\’, ‘/’, $file->getRealPath(卡塔尔卡塔尔(英语:State of Qatar);
//获取文件相对路线 $object = substr($filePath,
strlen($this->resRootDir卡塔尔国卡塔尔(قطر‎; //总括文件的版本号 $state =
$this->_getResStateVersion($filePath卡塔尔; //获取文件的多少个参数值 if
(true !== $this->getObjectInfo($object, $minObject, $needCompress,
$state, $extension卡塔尔(قطر‎卡塔尔 { continue; } //压缩文件的相对路线 $minFilePath =
str_replace(‘\\’, ‘/’, $this->resRootDir. $minObject);
//************此处p剖断是最重超越百分之五十之豆蔻梢头*****************//
//推断文件是不是存在且早就改成过 if (isset($resState[$object]) &&
$resState[$object] == $state && isset($resState[$minObject]) &&
file_exists($minFile帕特h卡塔尔国卡塔尔(英语:State of Qatar) { continue; } //确认保障/www/min/目录可写
$this->_ensureWritableDir(dirname($minFilePath)); if ($needCompress)
{ $this->compressResFileAndSave($filePath, $minFilePath); } else {
copy($filePath, $minFilePath); } $resState[$object] = $state;
$resState[$minObject] = ”; $count++; if ($count == 50) {
$this->_saveResState($resState); $count = 0; } } } if($count)
$this->_saveResState($resState卡塔尔; } public function
getObjectInfo($object, &$minObject, &$needCompress, &$state,
&$extension卡塔尔 { //获取财富相对路线 $filePath = $this->resRootDir .
$object; //剖断能源是或不是留存 if (!file_exists($filePath卡塔尔卡塔尔国 return
“能源文件不设有{$filePath}”; //版本号 $state = $this->
_getResStateVersion($filePath卡塔尔(英语:State of Qatar); //文件名后缀 $extension =
pathinfo($filePath, PATHINFO_EXTENSION卡塔尔(قطر‎; //是不是要裁减 $needCompress =
true; //推断财富文件是还是不是是以 .min.css或许.min.js结尾的
//此类结尾平常都以已回退过,比如jquery.min.js,就不要再压缩了 if
(str_end_with($object, ‘.min.’.$extension, true卡塔尔卡塔尔(قطر‎ {
//压缩后的能源贮存路线,放在 /www/min/ 目录下 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔(قطر‎ – strlen($extension卡塔尔(英语:State of Qatar) – 4卡塔尔(英语:State of Qatar) .
$state .’.’. $extension; $needCompress = false; } else if
(in_array($extension, $this->compressResDir卡塔尔(英语:State of Qatar)卡塔尔(قطر‎ {
//此处是索要减弱的文件目录 $minObject = ‘min/’.substr($object, 0,
strlen($object卡塔尔(قطر‎ – strlen($extension卡塔尔卡塔尔国 . $state . ‘.’ . $extension;
//看看是不是是忽视的门路前缀 foreach ($this->compressResIngorePrefix as
$v卡塔尔 { if (str_start_with($object, $v, true)) { $needCompress = false;
} } } else { $minObject = ‘min/’.$object; $needCompress = false; }
return true; } /** * 获取寄放能源版本的文本 * 它是身处贰个数组里 *
$resState = array( * ‘文件路线’ => ‘对应的本子号’, * ‘文件路线’
=> ‘对应的版本号’, * ‘文件路线’ => ‘对应的本子号’, * ); *
@return [巨型网址优化技巧,多图片上传程序代码。type] [description] */ public function getResState() { if
(file_exists($this->resStatePath)) { require $this->resStatePath;
return $resState; } return []; } /** *
总括文件的本子号,那几个是依照总计文件MD5散列值获得版本号 *
只要文件内容更换了,所总括拿到的散列值就能不一致等 *
用于判定能源文件是不是有转移过 * @param [type] $filePath
[description] * @return [type] [description] */ public function
_getResStateVersion($filePath) { return
base_convert(crc32(md5_file($filePath)), 10, 36); } /** *
确认保障目录可写 * @param [type] $dir [description] * @return [type]
[description] */ private function _ensureWritableDir($dir) { if
(!file_exists($dir)) { @mkdir($dir, 0777, true); @chmod($dir, 0777); }
else if (!is_writable($dir)) { @chmod($dir, 0777); if
(!is_writable($dir)) { show_error(‘目录’.$dir.’不可写’); } } } /**
* 将压缩后的财富文件写入到/www/min/下去 * @param [type] $filePath
[description] * @param [type] $minFilePath [description] *
@return [type] [description] */ private function
compressResFileAndSave($filePath, $minFilePath) { if
(!file_put_contents($minFilePath,
$this->compressResFile($filePath))) {
//$CI->exceptions->show_exception(“写入文件{$minFilePath}失利”卡塔尔(英语:State of Qatar);
show_error(“写入文件{$minFilePath}失利”, -1卡塔尔(英语:State of Qatar); } } /** * 压缩能源文件
* @param [type] $filePath [description] * @return [type]
[description] */ private function compressResFile($filePath) {
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); if
($extension === ‘js’) { require_once ‘JShrink/Minifier.php’; return
\JShrink\Minifier::minify(file_get_contents($filePath)); } else if
($extension ===’css’) { $content = file_get_contents($filePath);
$content =
preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”,
$content); $content = str_replace([“\r\n”, “\r”, “\n”], ”,
$content); $content = preg_replace(‘/([{}),;:>])\s+/’, ‘$1’,
$content); $content = preg_replace(‘/\s+([{}),;:>])/’, ‘$1’,
$content); $content = str_replace(‘;}’, ‘}’, $content); return
$content; } else {
//$CI->exceptions->show_exception(“不扶持压缩{extension}文件[$filePath]”);
show_error(“不支持压缩{extension}文件[$filePath]”, -1); } } private
function _saveResState($resState) { ksort($resState); $content =
“<?php\n\n\$resState = array(\n”; foreach ($resState as $k =>
$v) { $content .= “\t ‘$k’ => ‘$v’,\n”; } $content .= “);\n\n”;
file_put_contents($this->resStatePath, $content卡塔尔; } } 点击打开能源压缩类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
/**
* 资源压缩类
*/
class ResMinifier {
    /** 需要压缩的资源目录*/
    public $compressResDir = [‘css’, ‘js’];
    /** 忽略压缩的路径,例如此处是js/icon开头的路径忽略压缩*/
    public $compressResIngorePrefix = [‘js/icon’];
    /** 资源根目录*/
    public $resRootDir;
    /** 资源版本文件路径*/
    private $resStatePath;
 
    public function __construct() {
        $this->resRootDir = WEBROOT . ‘www/’;
        $this->resStatePath = WEBROOT . ‘www/resState.php’;
    }
 
    public function compressRes() {
        //获取存放版本的资源文件
        $resState = $this->getResState();
        $count = 0;
 
        //开始遍历需要压缩的资源目录
        foreach ($this->compressResDir as $resDir) {
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->resRootDir . $resDir , FilesystemIterator::SKIP_DOTS)) as $file) {
                //获取该资源文件的绝对路径
                $filePath = str_replace(‘\\’, ‘/’, $file->getRealPath());
                //获取文件相对路径
                $object = substr($filePath, strlen($this->resRootDir));
                //计算文件的版本号
                $state = $this->_getResStateVersion($filePath);
 
                //获取文件的几个参数值
                if (true !== $this->getObjectInfo($object, $minObject, $needCompress, $state, $extension)) {
                    continue;
                }
 
                //压缩文件的绝对路径
                $minFilePath = str_replace(‘\\’, ‘/’, $this->resRootDir. $minObject);
 
                //************此处p判断是最重要部分之一*****************//
                //判断文件是否存在且已经改动过
                if (isset($resState[$object]) && $resState[$object] == $state && isset($resState[$minObject]) && file_exists($minFilePath)) {
                    continue;
                }
 
                //确保/www/min/目录可写
                $this->_ensureWritableDir(dirname($minFilePath));
 
                if ($needCompress) {
                    $this->compressResFileAndSave($filePath, $minFilePath);
                } else {
                    copy($filePath, $minFilePath);
                }
 
                $resState[$object] = $state;
                $resState[$minObject] = ”;
                $count++;
 
                if ($count == 50) {
                    $this->_saveResState($resState);
                    $count = 0;
                }
 
            }
        }
        if($count) $this->_saveResState($resState);
    }
 
    public function getObjectInfo($object, &$minObject, &$needCompress, &$state, &$extension) {
        //获取资源绝对路径
        $filePath = $this->resRootDir . $object;
        //判断资源是否存在
        if (!file_exists($filePath)) return "资源文件不存在{$filePath}";
        //版本号
        $state = $this-> _getResStateVersion($filePath);
        //文件名后缀
        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
        //是否要压缩
        $needCompress = true;
 
        //判断资源文件是否是以 .min.css或者.min.js结尾的
        //此类结尾一般都是已压缩过,例如jquery.min.js,就不必再压缩了
        if (str_end_with($object, ‘.min.’.$extension, true)) {
            //压缩后的资源存放路径,放在 /www/min/ 目录下
            $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension) – 4) . $state .’.’. $extension;
            $needCompress = false;
        } else if (in_array($extension, $this->compressResDir)) {
            //此处是需要压缩的文件目录
            $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension)) . $state . ‘.’ . $extension;
            //看看是否是忽略的路径前缀
            foreach ($this->compressResIngorePrefix as $v) {
                if (str_start_with($object, $v, true)) {
                    $needCompress = false;
                }
            }
        } else {
            $minObject = ‘min/’.$object;
            $needCompress = false;
        }
        return true;
    }
 
    /**
     * 获取存放资源版本的文件
     * 它是放在一个数组里
     * $resState = array(
     *         ‘文件路径’ => ‘对应的版本号’,
     *         ‘文件路径’ => ‘对应的版本号’,
     *         ‘文件路径’ => ‘对应的版本号’,
     *     );
     * @return [type] [description]
     */
    public function getResState() {
        if (file_exists($this->resStatePath)) {
            require $this->resStatePath;
            return $resState;
        }
        return [];
    }
 
    /**
     * 计算文件的版本号,这个是根据计算文件MD5散列值得到版本号
     * 只要文件内容改变了,所计算得到的散列值就会不一样
     * 用于判断资源文件是否有改动过
     * @param  [type] $filePath [description]
     * @return [type]           [description]
     */
    public function _getResStateVersion($filePath) {
        return base_convert(crc32(md5_file($filePath)), 10, 36);
    }
 
    /**
     * 确保目录可写
     * @param  [type] $dir [description]
     * @return [type]      [description]
     */
    private function _ensureWritableDir($dir) {
        if (!file_exists($dir)) {
            @mkdir($dir, 0777, true);
            @chmod($dir, 0777);
        } else if (!is_writable($dir)) {
            @chmod($dir, 0777);
            if (!is_writable($dir)) {
                show_error(‘目录’.$dir.’不可写’);
            }
        }
    }
 
    /**
     * 将压缩后的资源文件写入到/www/min/下去
     * @param  [type] $filePath    [description]
     * @param  [type] $minFilePath [description]
     * @return [type]              [description]
     */
    private function compressResFileAndSave($filePath, $minFilePath) {
        if (!file_put_contents($minFilePath, $this->compressResFile($filePath))) {
 
            //$CI->exceptions->show_exception("写入文件{$minFilePath}失败");
            show_error("写入文件{$minFilePath}失败", -1);
        }
    }
 
    /**
     * 压缩资源文件
     * @param  [type] $filePath [description]
     * @return [type]           [description]
     */
    private function compressResFile($filePath) {
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        if ($extension === ‘js’) {
            require_once ‘JShrink/Minifier.php’;
            return \JShrink\Minifier::minify(file_get_contents($filePath));
        } else if ($extension ===’css’) {
            $content = file_get_contents($filePath);
            $content = preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”, $content);
            $content = str_replace(["\r\n", "\r", "\n"], ”, $content);
            $content = preg_replace(‘/([{}),;:>])\s+/’, ‘$1’, $content);
            $content = preg_replace(‘/\s+([{}),;:>])/’, ‘$1’, $content);
            $content = str_replace(‘;}’, ‘}’, $content);
            return $content;
        } else {
            //$CI->exceptions->show_exception("不支持压缩{extension}文件[$filePath]");
            show_error("不支持压缩{extension}文件[$filePath]", -1);
 
        }
    }
 
    private function _saveResState($resState) {
        ksort($resState);
        $content = "<?php\n\n\$resState = array(\n";
        foreach ($resState as $k => $v) {
            $content .= "\t ‘$k’ => ‘$v’,\n";
        }
        $content .= ");\n\n";
        file_put_contents($this->resStatePath, $content);
    }
 
}
 
点击打开 资源压缩类

全方位类大多数代码作者都加明白说,方便大家连忙通晓。这里作者也会对每大器晚成行代码实行解释。

(1)

PHP

/** 需求减少的财富目录*/ public $compressResDir = [‘css’, ‘js’];
/** 忽视压缩的路线,比方此处是js/icon最早的门径忽视压缩*/ public
$compressResIngorePrefix = [‘js/icon’]; /** 能源根目录*/ public
$resRootDir; /** 能源版本文件路线*/ private $resStatePath; public
function __construct() { $this->resRootDir = WEBROOT . ‘www/’;
$this->resStatePath = WEBROOT . ‘www/resState.php’; }

1
2
3
4
5
6
7
8
9
10
11
12
13
/** 需要压缩的资源目录*/
    public $compressResDir = [‘css’, ‘js’];
    /** 忽略压缩的路径,例如此处是js/icon开头的路径忽略压缩*/
    public $compressResIngorePrefix = [‘js/icon’];
    /** 资源根目录*/
    public $resRootDir;
    /** 资源版本文件路径*/
    private $resStatePath;
 
    public function __construct() {
        $this->resRootDir = WEBROOT . ‘www/’;
        $this->resStatePath = WEBROOT . ‘www/resState.php’;
    }

$compressResDir变量是要求减小的能源目录,要是你有新的拍卖目录,能够在那变量里即便新的目录名就可以管理。附上本人测验项目标目录图

澳门葡京 3

$compressResIngorePrefix 忽视被压缩的路子的路子前有些是该数组变量的字符串,举例有二个财富路线为
js/icon/bg.js或然是js/icon_index.js可能是js/icon.header.js,要是在该数组中投入了
js/icon这几个字符串,那么能源路线为js/icon开首的都会被忽略掉,也正是一贯跳过,不用压缩。(因为财富文件里总有大器晚成都部队分是不须求降低的呗)

$resRootDir寄放能源根目录的

$resStatePath 那么些是能源版本文件路线

(2)走入compressRes(卡塔尔(英语:State of Qatar) 方法,我们先深入分析前边那后生可畏段代码

PHP

public function compressRes(卡塔尔(قطر‎ { //获取存放版本的财富文件 $resState =
$this->getResState(卡塔尔(英语:State of Qatar); $count = 0;

1
2
3
4
public function compressRes() {
        //获取存放版本的资源文件
        $resState = $this->getResState();
        $count = 0;

——————————-调用getResState() 讲解start————————————————————-

那边首先是调用 $this->getResState(卡塔尔(قطر‎ 方法来博取存放版本的能源文件,此处先跳到该方式看看是哪些写的,其实就是包括该公文,然后回到里面存放版本号的数组,我们看注释可以看到该文件里存放版本号的格式(顺便附上海教室让大家看看)

PHP

/** * 获取贮存财富版本的公文 * 它是身处一个数组里 * $resState =
array( * ‘文件路线’ => ‘对应的本子号’, * ‘文件路线’ =>
‘对应的版本号’, * ‘文件路线’ => ‘对应的本子号’, * ); * @return
[type] [description] */ public function getResState() { if
(file_exists($this->resStatePath)) { require $this->resStatePath;
return $resState; } return []; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
     * 获取存放资源版本的文件
     * 它是放在一个数组里
     * $resState = array(
     *         ‘文件路径’ => ‘对应的版本号’,
     *         ‘文件路径’ => ‘对应的版本号’,
     *         ‘文件路径’ => ‘对应的版本号’,
     *     );
     * @return [type] [description]
     */
    public function getResState() {
        if (file_exists($this->resStatePath)) {
            require $this->resStatePath;
            return $resState;
        }
        return [];
    }

(资源版本文件截图:)

澳门葡京 4

——————————-调用getResState() 讲解end————————————————————-

进而看compressRes(卡塔尔(قطر‎里的那黄金时代段代码

PHP

//起头遍历须求减小的能源目录 foreach ($this->compressResDir as
$resDir卡塔尔 { foreach (new RecursiveIteratorIterator(new
RecursiveDirectoryIterator($this->resRootDir . $resDir ,
FilesystemIterator::SKIP_DOTS卡塔尔(英语:State of Qatar)卡塔尔 as $file卡塔尔(英语:State of Qatar) { //获取该能源文件的相对路线$filePath = str_replace(‘\\’, ‘/’, $file->getRealPath(卡塔尔卡塔尔国;
//获取文件相对路线 $object = substr($filePath,
strlen($this->resRootDir卡塔尔(قطر‎卡塔尔(قطر‎; //总计文件的本子号 $state =
$this->_getResStateVersion($filePath);

1
2
3
4
5
6
7
8
9
//开始遍历需要压缩的资源目录
        foreach ($this->compressResDir as $resDir) {
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->resRootDir . $resDir , FilesystemIterator::SKIP_DOTS)) as $file) {
                //获取该资源文件的绝对路径
                $filePath = str_replace(‘\\’, ‘/’, $file->getRealPath());
                //获取文件相对路径
                $object = substr($filePath, strlen($this->resRootDir));
                //计算文件的版本号
                $state = $this->_getResStateVersion($filePath);

第2个遍历的是js和css目录
第一个遍历是将js目录可能css目录里的公文都成为路线方式,

比如获取文件的绝对路线 $filePath 的值是那样子的:

/usr/local/apache2/htdocs/project/www/css/home/index.css

而文件的相对路径$object是那样子的 :

css/home/index.css

这里就从头调用$this->_getResStateVersion($filePath卡塔尔来计量文件的版本号

——————————-调用_getResStateVersion($filePath)
讲解start————————————————————

PHP

/** * 总括文件的本子号,这些是基于计算文件MD5散列值获得版本号 *
只要文件内容改换了,所总括拿到的散列值就能够不平等 *
用于决断财富文件是不是有改观过 * @param [type] $filePath
[description] * @return [type] [description] */ public function
_getResStateVersion($filePath) { return
base_convert(crc32(md5_file($filePath)), 10, 36); }

1
2
3
4
5
6
7
8
9
10
/**
     * 计算文件的版本号,这个是根据计算文件MD5散列值得到版本号
     * 只要文件内容改变了,所计算得到的散列值就会不一样
     * 用于判断资源文件是否有改动过
     * @param  [type] $filePath [description]
     * @return [type]           [description]
     */
    public function _getResStateVersion($filePath) {
        return base_convert(crc32(md5_file($filePath)), 10, 36);
    }

——————————-调用_getResStateVersion($filePath)
讲解end————————————————————-

抑或到版本号后,再看下少年老成段代码,这里发轫调用$this->getObjectInfo(卡塔尔国方法,这里收获到压缩文件的相对路线$minObject,是还是不是要求压缩$needCompress,版本号$state,文件后缀$extension。

PHP

//获取文件的多少个参数值 if (true !== $this->getObjectInfo($object,
$minObject, $needCompress, $state, $extension)) { continue; }

1
2
3
4
//获取文件的几个参数值
                if (true !== $this->getObjectInfo($object, $minObject, $needCompress, $state, $extension)) {
                    continue;
                }

——————————调用$this->getObjectInfo() 讲解start————————————————————

PHP

/** * 获取财富文件有关新闻 * @param [type] $object 财富文件路线(www/css/home/index.css卡塔尔(英语:State of Qatar) * @param [type] $minObject 压缩能源文件路线(www/min/css/home/index.ae123a.css卡塔尔(英语:State of Qatar) * @param [type] $needCompress
是还是不是要求压缩 * @param [type] $state 文件版本号 * @param [type]
$extension 文件名后缀 * @return [type] [description] */ public
function getObjectInfo($object, &$minObject, &$needCompress, &$state,
&$extension卡塔尔(英语:State of Qatar) { //获取财富相对路线 $filePath = $this->resRootDir .
$object; //判定财富是还是不是留存 if (!file_exists($file帕特h卡塔尔卡塔尔(英语:State of Qatar) return
“财富文件不设有{$filePath}”; //版本号 $state = $this->
_getResStateVersion($filePath卡塔尔; //文件名后缀 $extension =
pathinfo($filePath, PATHINFO_EXTENSION卡塔尔(英语:State of Qatar); //是不是要减小 $needCompress =
true; //剖断财富文件是不是是以 .min.css只怕.min.js结尾的
//此类结尾平常都以已裁减过,举例jquery.min.js,就不必再压缩了 if
(str_end_with($object, ‘.min.’.$extension, true卡塔尔(قطر‎卡塔尔(英语:State of Qatar) {
//压缩后的能源存放路线,放在 /www/min/ 目录下 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔国 – strlen($extension卡塔尔(قطر‎ – 4卡塔尔 .
$state .’.’. $extension; $needCompress = false; } else if
(in_array($extension, $this->compressResDir卡塔尔国卡塔尔(قطر‎ {
//此处是内需减小的文件目录 $minObject = ‘min/’.substr($object, 0,
strlen($object卡塔尔(قطر‎ – strlen($extension卡塔尔(英语:State of Qatar)卡塔尔 . $state . ‘.’ . $extension;
//看看是还是不是是忽视的门径前缀 foreach ($this->compressResIngorePrefix as
$v卡塔尔 { if (str_start_with($object, $v, true)) { $needCompress = false;
} } } else { $minObject = ‘min/’.$object; $needCompress = false; }
return true; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
     * 获取资源文件相关信息
     * @param  [type] $object       资源文件路径 (www/css/home/index.css)
     * @param  [type] $minObject    压缩资源文件路径 (www/min/css/home/index.ae123a.css)
     * @param  [type] $needCompress 是否需要压缩
     * @param  [type] $state        文件版本号
     * @param  [type] $extension    文件名后缀
     * @return [type]               [description]
     */
    public function getObjectInfo($object, &$minObject, &$needCompress, &$state, &$extension) {
        //获取资源绝对路径
        $filePath = $this->resRootDir . $object;
        //判断资源是否存在
        if (!file_exists($filePath)) return "资源文件不存在{$filePath}";
        //版本号
        $state = $this-> _getResStateVersion($filePath);
        //文件名后缀
        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
        //是否要压缩
        $needCompress = true;
 
        //判断资源文件是否是以 .min.css或者.min.js结尾的
        //此类结尾一般都是已压缩过,例如jquery.min.js,就不必再压缩了
        if (str_end_with($object, ‘.min.’.$extension, true)) {
            //压缩后的资源存放路径,放在 /www/min/ 目录下
            $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension) – 4) . $state .’.’. $extension;
            $needCompress = false;
        } else if (in_array($extension, $this->compressResDir)) {
            //此处是需要压缩的文件目录
            $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension)) . $state . ‘.’ . $extension;
            //看看是否是忽略的路径前缀
            foreach ($this->compressResIngorePrefix as $v) {
                if (str_start_with($object, $v, true)) {
                    $needCompress = false;
                }
            }
        } else {
            $minObject = ‘min/’.$object;
            $needCompress = false;
        }
        return true;
    }

本条方式里的每风姿浪漫行代码基本上都有注释了,所以就不一句句进行传授了,这里根本看下边包车型大巴论断部分:

if (str_澳门葡京 ,end_with($object, ‘.min.’.$extension, true卡塔尔(英语:State of Qatar)卡塔尔国那么些论断是相比较能源文件路线字串尾部是还是不是以 .min.$extension
结尾,比如是 jquery.min.js,这种文件本来正是
减少过的文件,所以就不用再开展裁减管理了, $minObject
那么些变量贮存的是削减后的财富文件路线。
此间附上str_end_with(卡塔尔(قطر‎函数的代码:

PHP

/** * 剖断 subject 是或不是以 search结尾, 参数钦赐是不是忽视大小写 *
@param [type] $subject [description] * @param [type] $search
[description] * @param boolean $ignore_case [description] *
@return [type] [description] */ function str_end_with($subject,
$search, $ignore_case = false) { $len2 = strlen($search); if (0 ===
$len2) return true; $len1 = strlen($subject); if ($len2 > $len1)
return false; if ($ignore_case) { return 0 === strcmp(substr($subject,
$len1 – $len2), $search); } else { return 0 ===
strcasecmp(substr($subject, $len1 – $len2), $search); } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
     * 判断 subject 是否以 search结尾, 参数指定是否忽略大小写
     * @param  [type]  $subject     [description]
     * @param  [type]  $search      [description]
     * @param  boolean $ignore_case [description]
     * @return [type]               [description]
     */
    function str_end_with($subject, $search, $ignore_case = false) {
        $len2 = strlen($search);
        if (0 === $len2) return true;
        $len1 = strlen($subject);
        if ($len2 > $len1) return false;
        if ($ignore_case) {
            return 0 === strcmp(substr($subject, $len1 – $len2), $search);
        } else {
            return 0 === strcasecmp(substr($subject, $len1 – $len2), $search);
        }
    }

if
(in_array($extension, $this->compressResDir卡塔尔(英语:State of Qatar),这些论断正是是还是不是是须求管理的多个目录里的。

然后里面的foreach ($this->compressResIngorePrefix as $v卡塔尔国 { if
(str_start_with($object, $v, true)) { $needCompress = false; } }

其一是决断是或不是是以$this->compressResIngorePrefix属性定义的先底部分字串早先的门径,是的话就大要压缩该能源文件。

认清到最终else
正是认证该财富文件不供给减小了,最终是再次来到$minObject,$needCompress,$state,$extension那多个变量。

——————————-调用$this->getObjectInfo() 讲解end————————————————————-

到此处继续回来看 compressRes(卡塔尔国方法里面的代码

PHP

//压缩文件的相对路线 $minFilePath = str_replace(‘\\’, ‘/’,
$this->resRootDir. $minObject);
//************此处p剖断是最要紧片段之大器晚成*****************//
//推断文件是还是不是存在且已经济体制改正变过 if (isset($resState[$object]) &&
$resState[$object] == $state && isset($resState[$minObject]) &&
file_exists($minFilePath)) { continue; }

1
2
3
4
5
6
7
8
//压缩文件的绝对路径
                $minFilePath = str_replace(‘\\’, ‘/’, $this->resRootDir. $minObject);
 
                //************此处p判断是最重要部分之一*****************//
                //判断文件是否存在且已经改动过
                if (isset($resState[$object]) && $resState[$object] == $state && isset($resState[$minObject]) && file_exists($minFilePath)) {
                    continue;
                }

这段代码首先是拼接出压缩文件的相对路径,

随后下边那一个推断是重大的片段,通过那么些论断就足以通晓该财富文件是不是被更修正,假使更修改的话,就再也对该财富文件举办减削,借使没改动过,就大浪涛沙管理下三个能源文件。看这里的论断:isset($resState[$object])
&& $resState[$object] == $state,那几个论断正是推断该文件路线是不是存在 
何况文件中对应的版本号和测算出的版本号是或不是还相近;isset($resState[$minObject])
&&file_exists($minFile帕特h卡塔尔,这一个是剖断压缩文件路线是还是不是存在,而且该压缩文件是或不是实际存在目录中。

看下风姿浪漫段代码,倘若能走到那生龙活虎局地,表达当前的那么些财富文件是被更修正的(代码改过过),那么那个时候就对文本进行压缩操作了

PHP

//确认保障/www/min/目录可写
$this->_ensureWritableDir(dirname($minFilePath)); if ($needCompress)
{ $this->compressResFileAndSave($filePath, $minFilePath); } else {
copy($filePath, $minFilePath); }

1
2
3
4
5
6
7
8
//确保/www/min/目录可写
                $this->_ensureWritableDir(dirname($minFilePath));
 
                if ($needCompress) {
                    $this->compressResFileAndSave($filePath, $minFilePath);
                } else {
                    copy($filePath, $minFilePath);
                }

$this->_ensureWritableDir(卡塔尔(قطر‎,此措施是要确认保证新成立的www/min目录是可写的,这里附上代码:

——————————-调用$this->_ensureWritableDir() 讲解start————————————————————-

PHP

/** * 确定保障目录可写 * @param [type] $dir [description] * @return
[type] [description] */ private function _ensureWritableDir($dir)
{ if (!file_exists($dir)) { @mkdir($dir, 0777, true); @chmod($dir,
0777); } else if (!is_writable($dir)) { @chmod($dir, 0777); if
(!is_writable($dir)) { show_error(‘目录’.$dir.’不可写’); } } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
     * 确保目录可写
     * @param  [type] $dir [description]
     * @return [type]      [description]
     */
    private function _ensureWritableDir($dir) {
        if (!file_exists($dir)) {
            @mkdir($dir, 0777, true);
            @chmod($dir, 0777);
        } else if (!is_writable($dir)) {
            @chmod($dir, 0777);
            if (!is_writable($dir)) {
                show_error(‘目录’.$dir.’不可写’);
            }
        }
    }

——————————-调用$this->_ensureWritableDir() 讲解end————————————————————-

if
($needCompress卡塔尔(英语:State of Qatar),那几个论断能源文件是或不是必要减小,须要的话调用$this->compressResFileAndSave($filePath,
$minFilePath卡塔尔(قطر‎;不需求的话,直接复制文件到压缩文件路线 copy($filePath,
$minFilePath卡塔尔(英语:State of Qatar);

先看$this->compressResFileAndSave()

——————————-调用$this->compressResFileAndSave()
讲解start————————————————————-

PHP

/** * 将核减后的财富文件写入到/www/min/下去 * @param [type]
$filePath [description] * @param [type] $minFilePath
[description] * @return [type] [description] */ private function
compressResFileAndSave($filePath, $minFilePath) { if
(!file_put_contents($minFilePath,
$this->compressResFile($filePath))) {
//$CI->exceptions->show_exception(“写入文件{$minFilePath}败北”卡塔尔(英语:State of Qatar);
show_error(“写入文件{$minFilePath}失利”, -1卡塔尔(英语:State of Qatar); } } /** * 压缩财富文件
* @param [type] $filePath [description] * @return [type]
[description] */ private function compressResFile($filePath) {
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); if
($extension === ‘js’) { require_once ‘JShrink/Minifier.php’; return
\JShrink\Minifier::minify(file_get_contents($filePath)); } else if
($extension ===’css’) { $content = file_get_contents($filePath);
$content =
preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”,
$content); $content = str_replace([“\r\n”, “\r”, “\n”], ”,
$content); $content = preg_replace(‘/([{}),;:>])\s+/’, ‘$1’,
$content); $content = preg_replace(‘/\s+([{}),;:>])/’, ‘$1’,
$content); $content = str_replace(‘;}’, ‘}’, $content); return
$content; } else {
//$CI->exceptions->show_exception(“不匡助压缩{extension}文件[$filePath]”);
show_error(“不帮衬压缩{extension}文件[$filePath]”, -1); } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
     * 将压缩后的资源文件写入到/www/min/下去
     * @param  [type] $filePath    [description]
     * @param  [type] $minFilePath [description]
     * @return [type]              [description]
     */
    private function compressResFileAndSave($filePath, $minFilePath) {
        if (!file_put_contents($minFilePath, $this->compressResFile($filePath))) {
 
            //$CI->exceptions->show_exception("写入文件{$minFilePath}失败");
            show_error("写入文件{$minFilePath}失败", -1);
        }
    }
 
    /**
     * 压缩资源文件
     * @param  [type] $filePath [description]
     * @return [type]           [description]
     */
    private function compressResFile($filePath) {
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        if ($extension === ‘js’) {
            require_once ‘JShrink/Minifier.php’;
            return \JShrink\Minifier::minify(file_get_contents($filePath));
        } else if ($extension ===’css’) {
            $content = file_get_contents($filePath);
            $content = preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”, $content);
            $content = str_replace(["\r\n", "\r", "\n"], ”, $content);
            $content = preg_replace(‘/([{}),;:>])\s+/’, ‘$1’, $content);
            $content = preg_replace(‘/\s+([{}),;:>])/’, ‘$1’, $content);
            $content = str_replace(‘;}’, ‘}’, $content);
            return $content;
        } else {
            //$CI->exceptions->show_exception("不支持压缩{extension}文件[$filePath]");
            show_error("不支持压缩{extension}文件[$filePath]", -1);
 
        }
    }

先裁减,再将裁减后的剧情写入到 压缩文件路线里去。

咱俩先看下那些压缩方法:

$this->compressResFile($filePath卡塔尔(英语:State of Qatar);
此方法中分两类压缩,第意气风发类时对js文件实行减少,第二类的对css文件举行减削。先说js压缩,这里是调用贰个JShrink的类,它

一个用来压缩js文件的PHP类,百度得以找到,调用那个类的minify(卡塔尔(قطر‎这么些方法就能够减小了;而css的回落利用正则替换到收缩,把那多个空格换行什么的都去掉。到此就减弱成功

了,然后再将减小后的财富写入到对应的压缩文件路线里去。

——————————-调用$this->compressResFileAndSave()
讲解end————————————————————-

随后继续看compressRes(卡塔尔这些措施里的代码,这里开始正是保存新的本子号到$resState数组里
$object=>$state,还应该有正是新的压缩路线$minObject,而这里$count++的效率是,当以此轮回肆拾肆次就将
$resState这几个数组写入壹回到
resState.php文件里,这里是由于严慎思虑而已,借使您不加这么些$count的管理那部分也能够,最终写入一遍就可以了。

PHP

$resState[$object] = $state; $resState[$minObject] = ”; $count++;
if ($count == 50) { $this->_saveResState($resState); $count = 0; } }
} if($count) $this->_saveResState($resState);

1
2
3
4
5
6
7
8
9
10
11
12
$resState[$object] = $state;
                $resState[$minObject] = ”;
                $count++;
 
                if ($count == 50) {
                    $this->_saveResState($resState);
                    $count = 0;
                }
 
            }
        }
        if($count) $this->_saveResState($resState);

这里看$this->_saveResState($resState卡塔尔,那一个法子就是将$resState数组写入到resState.php文件里去的主意。

——————————-调用$this->_saveResState($resState)
讲解start————————————————————-

PHP

private function _saveResState($resState) { ksort($resState); $content
= “<?php\n\n\$resState = array(\n”; foreach ($resState as $k
=> $v) { $content .= “\t ‘$k’ => ‘$v’,\n”; } $content .=
“);\n\n”; file_put_contents($this->resStatePath, $content); }

1
2
3
4
5
6
7
8
9
private function _saveResState($resState) {
        ksort($resState);
        $content = "<?php\n\n\$resState = array(\n";
        foreach ($resState as $k => $v) {
            $content .= "\t ‘$k’ => ‘$v’,\n";
        }
        $content .= ");\n\n";
        file_put_contents($this->resStatePath, $content);
    }

——————————-调用$this->_saveResState($resState)
讲解end————————————————————-

管理完后,看看所生成的公文,这里二个文本会有多个版本,旧版本尚未删除掉,在支付条件下删不删除都没难题,这里为何不删除旧版本的压缩文件,那就提到到在立异八个应用服务器代码时所要注意的难题里。在那作者就多讲授一点吗,轻便地举个例证吗,日常大型项目中的静态财富和模板文件是安插在分裂的机器集群上的,上线的进度中,静态财富和页面文件的配备时间距离或然会不长,对于三个巨型网络接收来讲正是在贰个非常小的年华间隔内,都有很大希望出现新客商采访,即便旧版本的静态能源删除了,但新本子的静态能源还未有陈设到位,那么顾客就加载不到该静态能源,结果简单的讲,所以,常常景况下大家会保留旧版本的静态能源,然后等具有片段安顿到位了,再通过自然的剧本删除掉也没提到,其实,这么些不必删除也是足以的,你思考,三个品类发二回版本,才会调用叁回能源文件裁减方法,它只会对更改良的文件实行生成新版本号的静态文件而已。这个就看个人的做法了。

澳门葡京 5

咱俩得以展开看看,上边这些正是减掉后的文件的代码了,文件原大小为16K,压缩后差十分少少了5K,今后是11K,压缩比大致是2/3,倘诺在大型项目中,叁个复杂点的页面会有极大的静态财富文件要加载,通过此方法,大大地增长了加载的快慢。(或者有个别朋友以为压缩个几K或然十几K算什么,完全能够忽视,其实笔者想说的是,当您在大型项目中优化项指标时候,能够减弱几K的代码,也给网址的属性升高了一大截)

澳门葡京 6

到此,能源减弱管理就解析实现了。其实,有一定幼功的朋友,能够直接看本人享受的要命代码就足以了,假诺掌握不了,再看本人下边这一步步的解析批注,小编是处在能看过来此博客的心上人,无论能力是好可能是稍弱,都能看懂,所以才对代码一步步地开展剖析传授。(希望各位多多点拨四哥)

————————————————————————————————————————-

  1. 接下去便是教师怎么着替换压缩后的能源文件了。

这个到Home.php

PHP

<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Home extends MY_Controller { public function index() {
$this->smartyData[‘test’] = 111; //那几个暗中同意是加载
www/css/home/index.css文件 $this->addResLink(‘index.css’卡塔尔(قطر‎;
//那么些暗许是加载www/js/jquery.all.min.js文件
$this->addResLink(‘/jquery.all.min.js’卡塔尔(英语:State of Qatar);
//那些暗中同意是加载www/js/index.js文件 $this->addResLink(‘index.js’卡塔尔国;
$this->displayView(‘home/index.tpl’卡塔尔(英语:State of Qatar); } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
 
class Home extends MY_Controller {
    public function index() {
        $this->smartyData[‘test’] = 111;
        //这个默认是加载 www/css/home/index.css文件
        $this->addResLink(‘index.css’);
        //这个默认是加载www/js/jquery.all.min.js文件
        $this->addResLink(‘/jquery.all.min.js’);
        //这个默认是加载www/js/index.js文件
        $this->addResLink(‘index.js’);
        $this->displayView(‘home/index.tpl’);
    }
}

地点有加载七个财富文件,大家先看看$this->addResLink(卡塔尔(英语:State of Qatar);那些方法,这一个法子放在My_Controller.php里:

PHP

/** * 财富路线 * @param [type] $filePath [description] */
protected function addResLink($filePath) { list($filePath, $query) =
explode(‘?’, $filePath . ‘?’); $extension =
strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); foreach
($this->_resLink as $v) { if (false === array_search($filePath,
$this->_resLink[$extension])) {
$this->_resLink[$extension][] = $query == null ? $filePath :
$filePath .’?’. $query; } } return $this; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
     * 资源路径
     * @param [type] $filePath [description]
     */
    protected function addResLink($filePath) {
        list($filePath, $query) = explode(‘?’, $filePath . ‘?’);
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        foreach ($this->_resLink as $v) {
            if (false === array_search($filePath, $this->_resLink[$extension])) {
                $this->_resLink[$extension][] = $query == null ? $filePath : $filePath .’?’. $query;
            }
        }
 
        return $this;
    }

此间关键是判别了财富文件是css照旧js,然后将其贮存在
$this->_resLink那些天性里。

那就是说这里作者就先附上My_Controller.php那个父类的具有代码吧

PHP

<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class MY_Controller extends CI_Controller { public function
__construct() { parent::__construct(卡塔尔(قطر‎; //压缩jscss财富文件
$this->compressResHandle(); }
//==========================使用SMARTY模板引擎================================//
/* 斯Matty母版页文件路线 */ protected $masterPage = ‘default.tpl’; /*
视图像和文字件路线*/ protected $smartyView; /* 要赋值给smarty视图的多少*/
protected $smartyData = []; /* 能源文件*/ protected $_resLink =
[‘js’=>[], ‘css’=>[]]; /** * 使用母版页输出二个视图 *
@return [type] [description] */ protected function
displayView($viewName = null, $masterPage = null卡塔尔国 { //为空则选用默许母版
if ($masterPage == null卡塔尔(英语:State of Qatar) $masterPage = $this->masterPage;
//获取视图的出口内容 $viewContent =
$this->_fetchView($this->smartyData, $viewName, $masterPage);
$output = ”; //添加css Link foreach ($this->_resLink[‘css’] as
$v) { $output .= res_link($v卡塔尔(英语:State of Qatar); } //内容部分 $output .= $viewContent;
//尾部增多js 链接 foreach ($this->_resLink[‘js’] as $v) { $output
.= res_link($v卡塔尔国; } //发送最终输出结果甚至服务器的 HTTP 头到浏览器
$this->output->_display($output); return $output; } private
function _fetchView($smartyData, &$viewName, &$masterPage) { if
($viewName == null) $viewName = $this->smartyView; if
(empty($this->smarty)) { require_once
SMARTY_DIR.’Smarty.class.php’; $this->smarty = new 斯Matty();
$this->smarty->setCompileDir(应用程式PATH . ‘cache/’卡塔尔国;
$this->smarty->setCacheDir(应用软件PATH . ‘cache/’卡塔尔国; }
//设置视图真实路线 $this->_getViewDir(true, $viewName, $masterPage,
$templateDir); foreach ($smartyData as $k => $v) {
$this->smarty->assign($k, $v); } if (empty($masterPage)) { return
$this->smarty->fetch($viewName); } else {
$this->smarty->assign(‘VIEW_MAIN’, $viewName); return
$this->smarty->fetch($masterPage); } } /** * 能源路线 * @param
[type] $filePath [description] */ protected function
addResLink($filePath) { list($filePath, $query) = explode(‘?’, $filePath
. ‘?’); $extension = strtolower(pathinfo($filePath,
PATHINFO_EXTENSION)); foreach ($this->_resLink as $v) { if (false
=== array_search($filePath, $this->_resLink[$extension])) {
$this->_resLink[$extension][] = $query == null ? $filePath :
$filePath .’?’. $query; } } return $this; } private function
_getViewDir($setTemplateDir, &$viewName, &$masterPage = null,
&$templateDir) { if (‘/’ === $viewName[0]卡塔尔(英语:State of Qatar) $viewName =
substr($viewName, 1卡塔尔(قطر‎; //是还是不是使用模板,有,则路由到
/views/master_page/*****.tpl下去 if ($masterPage) { $masterPage =
‘/’ === $masterPage[0] ? substr($masterPage, 1) : (‘master_page’
.’/’. $masterPage卡塔尔; } //是还是不是设置模板目录 if ($setTemplateDir卡塔尔 {
$templateDir = VIEWPATH;
$this->smarty->setTemplateDir($templateDir卡塔尔(قطر‎; } } /** *
压缩js、css财富文件(优化) * @return [type] [description] */
private function compressResHandle(卡塔尔国 {
$this->load->library(‘ResMinifier’); //压缩钦定文件夹下的财富文件
$this->resminifier->compressRes(卡塔尔; } } 点击打开 My_Controller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
 
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
 
        //压缩jscss资源文件
        $this->compressResHandle();
    }
 
    //==========================使用SMARTY模板引擎================================//
    /* Smarty母版页文件路径 */
    protected $masterPage = ‘default.tpl’;
    /* 视图文件路径*/
    protected $smartyView;
    /* 要赋值给smarty视图的数据*/
    protected $smartyData = [];
    /* 资源文件*/
    protected $_resLink = [‘js’=>[], ‘css’=>[]];
 
    /**
     * 使用母版页输出一个视图
     * @return [type] [description]
     */
    protected function displayView($viewName = null, $masterPage = null) {
        //为空则选用默认母版
        if ($masterPage == null) $masterPage = $this->masterPage;
        //获取视图的输出内容
        $viewContent = $this->_fetchView($this->smartyData, $viewName, $masterPage);
 
        $output = ”;
 
        //添加css Link
        foreach ($this->_resLink[‘css’] as $v) {
            $output .= res_link($v);
        }
 
        //内容部分
        $output .= $viewContent;
        //尾部添加js 链接
        foreach ($this->_resLink[‘js’] as $v) {
            $output .= res_link($v);
        }
        //发送最终输出结果以及服务器的 HTTP 头到浏览器
 
        $this->output->_display($output);
        return $output;
    }
 
    private function _fetchView($smartyData, &$viewName, &$masterPage) {
        if ($viewName == null) $viewName = $this->smartyView;
 
        if (empty($this->smarty)) {
            require_once SMARTY_DIR.’Smarty.class.php’;
            $this->smarty = new Smarty();
            $this->smarty->setCompileDir(APPPATH . ‘cache/’);
            $this->smarty->setCacheDir(APPPATH . ‘cache/’);
        }
 
        //设置视图真实路径
        $this->_getViewDir(true, $viewName, $masterPage, $templateDir);
 
        foreach ($smartyData as $k => $v) {
            $this->smarty->assign($k, $v);
        }
 
        if (empty($masterPage)) {
            return $this->smarty->fetch($viewName);
        } else {
            $this->smarty->assign(‘VIEW_MAIN’, $viewName);
            return $this->smarty->fetch($masterPage);
        }
    }
 
    /**
     * 资源路径
     * @param [type] $filePath [description]
     */
    protected function addResLink($filePath) {
        list($filePath, $query) = explode(‘?’, $filePath . ‘?’);
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        foreach ($this->_resLink as $v) {
            if (false === array_search($filePath, $this->_resLink[$extension])) {
                $this->_resLink[$extension][] = $query == null ? $filePath : $filePath .’?’. $query;
            }
        }
 
        return $this;
    }
 
    private function _getViewDir($setTemplateDir, &$viewName, &$masterPage = null, &$templateDir) {
        if (‘/’ === $viewName[0]) $viewName = substr($viewName, 1);
 
        //是否使用模板,有,则路由到 /views/master_page/*****.tpl下去
        if ($masterPage) {
            $masterPage = ‘/’ === $masterPage[0] ? substr($masterPage, 1) : (‘master_page’ .’/’. $masterPage);
        }
 
        //是否设置模板目录
        if ($setTemplateDir) {
            $templateDir = VIEWPATH;
            $this->smarty->setTemplateDir($templateDir);
        }
    }
 
    /**
     * 压缩js、css资源文件(优化)
     * @return [type] [description]
     */
    private function compressResHandle() {
        $this->load->library(‘ResMinifier’);
        //压缩指定文件夹下的资源文件
        $this->resminifier->compressRes();
    }
}
 
点击打开 My_Controller.php

打字与印刷出来 $this->_resLink那么些本性的布局是那样子的:

PHP

Array ( [js] => Array ( [0] => /jquery.all.min.js [1] =>
index.js ) [css] => Array ( [0] => index.css ) )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Array
(
    [js] => Array
        (
            [0] => /jquery.all.min.js
            [1] => index.js
        )
 
    [css] => Array
        (
            [0] => index.css
        )
 
)

再回到Home.php里面调用 $this->displayView(‘home/index.tpl’卡塔尔(قطر‎;

小编们看那么些办法:

PHP

/** * 使用母版页输出一个视图 * @return [type] [description] */
protected function displayView($viewName = null, $masterPage = null卡塔尔国 {
//为空则选拔私下认可母版 if ($masterPage == null卡塔尔国 $masterPage =
$this->masterPage; //获取视图的输出内容 $viewContent =
$this->_fetchView($this->smartyData, $viewName, $masterPage);
$output = ”; //添加css Link foreach ($this->_resLink[‘css’] as
$v) { $output .= res_link($v卡塔尔; } //内容部分 $output .= $viewContent;
//尾巴部分增加js 链接 foreach ($this->_resLink[‘js’] as $v) { $output
.= res_link($v卡塔尔; } //发送最终输出结果以致服务器的 HTTP 头到浏览器
$this->output->_display($output); return $output; } private
function _fetchView($smartyData, &$viewName, &$masterPage) { if
($viewName == null) $viewName = $this->smartyView; if
(empty($this->smarty)) { require_once
SMARTY_DICR-V.’斯Matty.class.php’; $this->smarty = new 斯Matty(卡塔尔国;
$this->smarty->setCompileDir(应用程式PATH . ‘cache/’卡塔尔(قطر‎;
$this->smarty->setCacheDir(应用程式PATH . ‘cache/’卡塔尔国; }
//设置视图真实路线 $this->_getViewDir(true, $viewName, $masterPage,
$templateDir); foreach ($smartyData as $k => $v) {
$this->smarty->assign($k, $v); } if (empty($masterPage)) { return
$this->smarty->fetch($viewName); } else {
$this->smarty->assign(‘VIEW_MAIN’, $viewName); return
$this->smarty->fetch($masterPage); } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
     * 使用母版页输出一个视图
     * @return [type] [description]
     */
    protected function displayView($viewName = null, $masterPage = null) {
        //为空则选用默认母版
        if ($masterPage == null) $masterPage = $this->masterPage;
        //获取视图的输出内容
        $viewContent = $this->_fetchView($this->smartyData, $viewName, $masterPage);
 
        $output = ”;
 
        //添加css Link
        foreach ($this->_resLink[‘css’] as $v) {
            $output .= res_link($v);
        }
 
        //内容部分
        $output .= $viewContent;
        //尾部添加js 链接
        foreach ($this->_resLink[‘js’] as $v) {
            $output .= res_link($v);
        }
        //发送最终输出结果以及服务器的 HTTP 头到浏览器
 
        $this->output->_display($output);
        return $output;
    }
 
    private function _fetchView($smartyData, &$viewName, &$masterPage) {
        if ($viewName == null) $viewName = $this->smartyView;
 
        if (empty($this->smarty)) {
            require_once SMARTY_DIR.’Smarty.class.php’;
            $this->smarty = new Smarty();
            $this->smarty->setCompileDir(APPPATH . ‘cache/’);
            $this->smarty->setCacheDir(APPPATH . ‘cache/’);
        }
 
        //设置视图真实路径
        $this->_getViewDir(true, $viewName, $masterPage, $templateDir);
 
        foreach ($smartyData as $k => $v) {
            $this->smarty->assign($k, $v);
        }
 
        if (empty($masterPage)) {
            return $this->smarty->fetch($viewName);
        } else {
            $this->smarty->assign(‘VIEW_MAIN’, $viewName);
            return $this->smarty->fetch($masterPage);
        }
    }

那大器晚成段代码未有意气风发部分正是调用了Smarty模板引擎的内容,这一个关于斯Matty的文化作者就不讲了,我们能够和谐百度,这里首要讲
res_link(卡塔尔国那么些函数,便是经过那么些函数来进行能源文件替换的。先看这几个函数的代码:

PHP

/** * 输出 HttpHead 中的财富总是。 css/js 自动判定真实路线 * @param
string 文件路线 * @return string */ function res_link($file) { $file
= res_path($file, $extension); if ($extension === ‘css’) { return
‘<link rel=”stylesheet” type=”text/css” href=”‘ . $file . ‘”/>’; }
else if ($extension === ‘js’) { return ‘<script
type=”text/javascript” src=”‘.$file.'”></script>’; } else {
return false; } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
     * 输出 HttpHead 中的资源连接。 css/js 自动判断真实路径
     * @param  string  文件路径
     * @return string      
     */
    function res_link($file) {
        $file = res_path($file, $extension);
 
        if ($extension === ‘css’) {
           return ‘<link rel="stylesheet" type="text/css" href="’ . $file . ‘"/>’;
        } else if ($extension === ‘js’) {
            return ‘<script type="text/javascript" src="’.$file.’"></script>’;
        } else {
            return false;
        }
    }

此间最根本便是 res_path() 函数了,那几个函数能半自动路由财富的真人真事路线。举例:index.css = > css/home/index.css

该函数最注重的三个功能是替换财富的回退版本。

平昔看代码:

PHP

/** * 智能路由财富实际路线 * @param string 路径 * @param string
扩展名 * @return string 真实路线 */ function res_path($file,
&$extension卡塔尔国 { //检查是否存在询问字符串 list($file, $query卡塔尔 =
explode(‘?’, $file . ‘?’卡塔尔国; //得到扩大名 $extension =
strtolower(pathinfo($file, PATHINFO_EXTENSION)); // $file =
str_replace(‘\\’, ‘/’, $file卡塔尔国; //取妥善前调控器名 global $class; if
($class == null卡塔尔(قطر‎ exit(‘can not get class name’卡塔尔(英语:State of Qatar); $className =
strtolower($class卡塔尔; //此处的平整是这么: //比方,假使不加 /
,Home调节器对应的格式是: index.css,那么
此处的路线会形成css/home/index.css //要是有 / ,调整器的格式能够是
/main.css,那么这里的路线会产生 css/main.css(公用的css类) if (‘/’ !==
$file[0]卡塔尔(قطر‎ { //index.css => css/home/index.css $object = $extension
.’/’. $className .’/’ . $file; } else { // /css/main.css 大概 /main.css
=> css/main.css $object = substr($file, 1卡塔尔; //若object是 main.css
,则自动抬高 扩展名目录 => css/main.css if (0 !==
strncasecmp($extension, $object, strlen($extension卡塔尔(قطر‎卡塔尔(英语:State of Qatar)卡塔尔(قطر‎ { $object =
$extension . ‘/’ . $object; } } //能源真实路线 $filepath =
WEBROOT.’www/’.$object; //替换压缩版本,那生龙活虎部分逻辑与公事降低逻辑对应 if
(in_array($extension, array(‘css’, ‘js’))) {
if(!str_start_with($object, ‘min/’) &&
file_exists(APPPATH.’libraries/ResMinifier.php’)) { require_once
APPPATH.’libraries/ResMinifier.php’; $resminifier = new ResMinifier(卡塔尔(英语:State of Qatar);
//获取贮存资源版本的文书的数组变量 $resState =
$resminifier->getResState(卡塔尔(英语:State of Qatar); //总计取稳当前文件版本号 $state =
$resminifier->_getResStateVersion($filepath卡塔尔; //剖断该版本号是还是不是存在
if (isset($resState[$object]卡塔尔(英语:State of Qatar)卡塔尔国 { //判别是或不是是.min.css或.min.js结尾 if
(str_end_with($object, ‘.min.’.$extension卡塔尔国卡塔尔 {
//将版本号拼接上去,然后拿走min的文件路线 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔(قطر‎ – strlen($extension卡塔尔国 – 4卡塔尔(英语:State of Qatar) .
$state . ‘.’ . $extension; } else {
//将版本号拼接上去,然后拿走min的文本路线 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔(قطر‎ – strlen($extension卡塔尔卡塔尔国 . $state
. ‘.’ . $extension; } //剖断min的门道是不是存在在$resState里面 if
(isset($resState[$minObject])) { $object = $minObject; $query = ”; }
} } $file = RES_BASE_URL . $object; } return ($query == null) ? $file
: ($file .’?’. $query); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
     * 智能路由资源真实路径
     * @param  string      路径
     * @param  string      扩展名
     * @return string       真实路径
     */
    function res_path($file, &$extension) {
        //检查是否存在查询字符串
        list($file, $query) = explode(‘?’, $file . ‘?’);
        //取得扩展名
        $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        //
        $file = str_replace(‘\\’, ‘/’, $file);
        //取得当前控制器名
        global $class;
        if ($class == null) exit(‘can not get class name’);
        $className = strtolower($class);
 
        //此处的规则是这样:
        //例如,如果不加 / ,Home控制器对应的格式是: index.css,那么 此处的路径会变成css/home/index.css
        //假如有 / ,控制器的格式可以是 /main.css,那么此处的路径会变成 css/main.css(公用的css类)
        if (‘/’ !== $file[0]) {
            //index.css => css/home/index.css
            $object = $extension .’/’. $className .’/’ . $file;
        } else {
            // /css/main.css 或者 /main.css => css/main.css
            $object = substr($file, 1);
 
            //若object是 main.css ,则自动加上 扩展名目录 => css/main.css
            if (0 !== strncasecmp($extension, $object, strlen($extension))) {
                $object = $extension . ‘/’ . $object;
            }
        }
        //资源真实路径
        $filepath = WEBROOT.’www/’.$object;
 
        //替换压缩版本,这部分逻辑与文件压缩逻辑对应
        if (in_array($extension, array(‘css’, ‘js’))) {
            if(!str_start_with($object, ‘min/’) && file_exists(APPPATH.’libraries/ResMinifier.php’)) {
                require_once APPPATH.’libraries/ResMinifier.php’;
                $resminifier = new ResMinifier();
                //获取存放资源版本的文件的数组变量
                $resState = $resminifier->getResState();
                //计算得到当前文件版本号
                $state = $resminifier->_getResStateVersion($filepath);
                //判断该版本号是否存在
                if (isset($resState[$object])) {
                    //判断是否是.min.css或.min.js结尾
                    if (str_end_with($object, ‘.min.’.$extension)) {
                        //将版本号拼接上去,然后得到min的文件路径
                        $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension) – 4) . $state . ‘.’ . $extension;
                    } else {
                        //将版本号拼接上去,然后得到min的文件路径
                        $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension)) . $state . ‘.’ . $extension;
                    }
                    //判断min的路径是否存在在$resState里面
                     if (isset($resState[$minObject])) {
                        $object = $minObject;
                        $query = ”;
                     }
                }
 
            }
 
            $file = RES_BASE_URL . $object;
        }
 
        return ($query == null) ? $file : ($file .’?’. $query);
 
    }

代码基本上都给了批注,方便我们轻便去领略,前面风华正茂部分是智能路线css、js能源的不二诀窍,前面后生可畏部分是替换压缩版本,这大器晚成某些的逻辑其实和能源缩小这里的逻辑基本风姿洒脱致,正是通过财富文件路线,举行判别和管理,最终收获能源的回降版本的门道,最终就将能源的收缩版本的路径重返去,放在'<link
rel=”stylesheet” type=”text/css” href=”‘ . $file . ‘”/>’里面。这样 
,就成功地将财富文件路线替换到了收缩版本的财富文件路线,并且在模板输出时,输出的是缩小后的财富文件。

到此,财富替换的从头到尾的经过就到此批注截止。而整生机勃勃项本领也剖析到此。

三、总结

在那笔者聚集地附着本博文疏解中的多少个文本代码:

Home.php

PHP

<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Home extends MY_Controller { public function index() {
$this->smartyData[‘test’] = 111; //这几个私下认可是加载
www/css/home/index.css文件 $this->addResLink(‘index.css’卡塔尔国;
//那些私下认可是加载www/js/jquery.all.min.js文件
$this->addResLink(‘/jquery.all.min.js’卡塔尔国;
//这些暗许是加载www/js/index.js文件 $this->addResLink(‘index.js’卡塔尔;
$this->displayView(‘home/index.tpl’卡塔尔国; } } 点击张开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
 
class Home extends MY_Controller {
    public function index() {
        $this->smartyData[‘test’] = 111;
        //这个默认是加载 www/css/home/index.css文件
        $this->addResLink(‘index.css’);
        //这个默认是加载www/js/jquery.all.min.js文件
        $this->addResLink(‘/jquery.all.min.js’);
        //这个默认是加载www/js/index.js文件
        $this->addResLink(‘index.js’);
        $this->displayView(‘home/index.tpl’);
    }
}
 
点击打开

My_Controller.php

PHP

<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class MY_Controller extends CI_Controller { public function
__construct() { parent::__construct(卡塔尔(英语:State of Qatar); //压缩jscss财富文件
$this->compressResHandle(卡塔尔国; }
//==========================使用SMARTY模板引擎================================//
/* 斯Matty母版页文件路径 */ protected $masterPage = ‘default.tpl’; /*
视图像和文字件路线*/ protected $smartyView; /* 要赋值给smarty视图的多少*/
protected $smartyData = []; /* 财富文件*/ protected $_resLink =
[‘js’=>[], ‘css’=>[]]; /** * 使用母版页输出四个视图 *
@return [type] [description] */ protected function
displayView($viewName = null, $masterPage = null卡塔尔国 { //为空则选择默许母版
if ($masterPage == null卡塔尔国 $masterPage = $this->masterPage;
//获取视图的出口内容 $viewContent =
$this->_fetchView($this->smartyData, $viewName, $masterPage);
$output = ”; //添加css Link foreach ($this->_resLink[‘css’] as
$v) { $output .= res_link($v卡塔尔国; } //内容部分 $output .= $viewContent;
//尾巴部分增加js 链接 foreach ($this->_resLink[‘js’] as $v) { $output
.= res_link($v卡塔尔(قطر‎; } //发送最终输出结果以至服务器的 HTTP 头到浏览器
$this->output->_display($output); return $output; } private
function _fetchView($smartyData, &$viewName, &$masterPage) { if
($viewName == null) $viewName = $this->smartyView; if
(empty($this->smarty)) { require_once
SMARTY_DICRUISER.’Smarty.class.php’; $this->smarty = new Smarty(卡塔尔(英语:State of Qatar);
$this->smarty->setCompileDir(APPPATH . ‘cache/’卡塔尔(英语:State of Qatar);
$this->smarty->setCacheDir(应用程式PATH . ‘cache/’卡塔尔国; }
//设置视图真实路径 $this->_getViewDir(true, $viewName, $masterPage,
$templateDir); foreach ($smartyData as $k => $v) {
$this->smarty->assign($k, $v); } if (empty($masterPage)) { return
$this->smarty->fetch($viewName); } else {
$this->smarty->assign(‘VIEW_MAIN’, $viewName); return
$this->smarty->fetch($masterPage); } } /** * 能源路线 * @param
[type] $filePath [description] */ protected function
addResLink($filePath) { list($filePath, $query) = explode(‘?’, $filePath
. ‘?’); $extension = strtolower(pathinfo($filePath,
PATHINFO_EXTENSION)); foreach ($this->_resLink as $v) { if (false
=== array_search($filePath, $this->_resLink[$extension])) {
$this->_resLink[$extension][] = $query == null ? $filePath :
$filePath .’?’. $query; } } return $this; } private function
_getViewDir($setTemplateDir, &$viewName, &$masterPage = null,
&$templateDir) { if (‘/’ === $viewName[0]卡塔尔(英语:State of Qatar) $viewName =
substr($viewName, 1卡塔尔(英语:State of Qatar); //是还是不是选用模板,有,则路由到
/views/master_page/*****.tpl下去 if ($masterPage) { $masterPage =
‘/’ === $masterPage[0] ? substr($masterPage, 1) : (‘master_page’
.’/’. $masterPage卡塔尔(قطر‎; } //是不是设置模板目录 if ($setTemplateDir卡塔尔 {
$templateDir = VIEWPATH;
$this->smarty->setTemplateDir($templateDir卡塔尔国; } } /** *
压缩js、css财富文件(优化) * @return [type] [description] */
private function compressResHandle(卡塔尔 {
$this->load->library(‘Res迷你fier’卡塔尔; //压缩钦赐文件夹下的能源文件
$this->resminifier->compressRes(卡塔尔; } } 点击张开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
 
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
 
        //压缩jscss资源文件
        $this->compressResHandle();
    }
 
    //==========================使用SMARTY模板引擎================================//
    /* Smarty母版页文件路径 */
    protected $masterPage = ‘default.tpl’;
    /* 视图文件路径*/
    protected $smartyView;
    /* 要赋值给smarty视图的数据*/
    protected $smartyData = [];
    /* 资源文件*/
    protected $_resLink = [‘js’=>[], ‘css’=>[]];
 
    /**
     * 使用母版页输出一个视图
     * @return [type] [description]
     */
    protected function displayView($viewName = null, $masterPage = null) {
        //为空则选用默认母版
        if ($masterPage == null) $masterPage = $this->masterPage;
        //获取视图的输出内容
        $viewContent = $this->_fetchView($this->smartyData, $viewName, $masterPage);
 
        $output = ”;
 
        //添加css Link
        foreach ($this->_resLink[‘css’] as $v) {
            $output .= res_link($v);
        }
 
        //内容部分
        $output .= $viewContent;
        //尾部添加js 链接
        foreach ($this->_resLink[‘js’] as $v) {
            $output .= res_link($v);
        }
        //发送最终输出结果以及服务器的 HTTP 头到浏览器
 
        $this->output->_display($output);
        return $output;
    }
 
    private function _fetchView($smartyData, &$viewName, &$masterPage) {
        if ($viewName == null) $viewName = $this->smartyView;
 
        if (empty($this->smarty)) {
            require_once SMARTY_DIR.’Smarty.class.php’;
            $this->smarty = new Smarty();
            $this->smarty->setCompileDir(APPPATH . ‘cache/’);
            $this->smarty->setCacheDir(APPPATH . ‘cache/’);
        }
 
        //设置视图真实路径
        $this->_getViewDir(true, $viewName, $masterPage, $templateDir);
 
        foreach ($smartyData as $k => $v) {
            $this->smarty->assign($k, $v);
        }
 
        if (empty($masterPage)) {
            return $this->smarty->fetch($viewName);
        } else {
            $this->smarty->assign(‘VIEW_MAIN’, $viewName);
            return $this->smarty->fetch($masterPage);
        }
    }
 
    /**
     * 资源路径
     * @param [type] $filePath [description]
     */
    protected function addResLink($filePath) {
        list($filePath, $query) = explode(‘?’, $filePath . ‘?’);
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        foreach ($this->_resLink as $v) {
            if (false === array_search($filePath, $this->_resLink[$extension])) {
                $this->_resLink[$extension][] = $query == null ? $filePath : $filePath .’?’. $query;
            }
        }
 
        return $this;
    }
 
    private function _getViewDir($setTemplateDir, &$viewName, &$masterPage = null, &$templateDir) {
        if (‘/’ === $viewName[0]) $viewName = substr($viewName, 1);
 
        //是否使用模板,有,则路由到 /views/master_page/*****.tpl下去
        if ($masterPage) {
            $masterPage = ‘/’ === $masterPage[0] ? substr($masterPage, 1) : (‘master_page’ .’/’. $masterPage);
        }
 
        //是否设置模板目录
        if ($setTemplateDir) {
            $templateDir = VIEWPATH;
            $this->smarty->setTemplateDir($templateDir);
        }
    }
 
    /**
     * 压缩js、css资源文件(优化)
     * @return [type] [description]
     */
    private function compressResHandle() {
        $this->load->library(‘ResMinifier’);
        //压缩指定文件夹下的资源文件
        $this->resminifier->compressRes();
    }
}
 
点击打开

ResMinifier.php

PHP

<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
/** * 能源压缩类 */ class ResMinifier { /** 必要降低的财富目录*/
public $compressResDir = [‘css’, ‘js’]; /**
忽视压缩的门道,比方此处是js/icon最初的路线忽视压缩*/ public
$compressResIngorePrefix = [‘js/icon’]; /** 能源根目录*/ public
$resRootDir; /** 资源版本文件路线*/ private $resStatePath; public
function __construct(卡塔尔 { $this->resRootDir = WEBROOT . ‘www/’;
$this->resStatePath = WEBROOT . ‘www/resState.php’; } public function
compressRes(卡塔尔(قطر‎ { //获取贮存版本的能源文件 $resState =
$this->getResState(卡塔尔(قطر‎; $count = 0; //起初遍历要求减小的能源目录
foreach ($this->compressResDir as $resDir卡塔尔国 { foreach (new
RecursiveIteratorIterator(new
RecursiveDirectoryIterator($this->resRootDir . $resDir ,
FilesystemIterator::SKIP_DOTS卡塔尔卡塔尔(英语:State of Qatar) as $file卡塔尔 { //获取该财富文件的绝对路线$filePath = str_replace(‘\\’, ‘/’, $file->getReal帕特h(卡塔尔(قطر‎卡塔尔(英语:State of Qatar);
//获取文件相对路线 $object = substr($filePath,
strlen($this->resRootDir卡塔尔卡塔尔(قطر‎; //总结文件的本子号 $state =
$this->_getResStateVersion($filePath卡塔尔(英语:State of Qatar); //获取文件的多少个参数值 if
(true !== $this->getObjectInfo($object, $minObject, $needCompress,
$state, $extension卡塔尔(قطر‎卡塔尔国 { continue; } //压缩文件的相对路线 $minFilePath =
str_replace(‘\\’, ‘/’, $this->resRootDir. $minObject);
//************此处p判别是最主要部分之黄金时代*****************//
//剖断文件是还是不是留存且早就改成过 if (isset($resState[$object]) &&
$resState[$object] == $state && isset($resState[$minObject]) &&
file_exists($minFilePath卡塔尔(قطر‎卡塔尔(英语:State of Qatar) { continue; } //确定保障/www/min/目录可写
$this->_ensureWritableDir(dirname($minFilePath)); if ($needCompress)
{ $this->compressResFileAndSave($filePath, $minFilePath); } else {
copy($filePath, $minFilePath); } $resState[$object] = $state;
$resState[$minObject] = ”; $count++; if ($count == 50) {
$this->_saveResState($resState); $count = 0; } } } if($count)
$this->_saveResState($resState); } /** * 获取能源文件有关音讯 *
@param [type] $object 财富文件路线 (www/css/home/index.css卡塔尔 * @param
[type] $minObject 压缩能源文件路线 (www/min/css/home/index.ae123a.css卡塔尔(قطر‎
* @param [type] $needCompress 是不是要求压缩 * @param [type] $state
文件版本号 * @param [type] $extension 文件名后缀 * @return [type]
[description] */ public function getObjectInfo($object, &$minObject,
&$needCompress, &$state, &$extension卡塔尔(قطر‎ { //获取能源相对路线 $filePath =
$this->resRootDir . $object; //判定财富是还是不是存在 if
(!file_exists($filePath卡塔尔(英语:State of Qatar)卡塔尔 return “能源文件子虚乌有{$filePath}”; //版本号
$state = $this-> _getResStateVersion($filePath卡塔尔国; //文件名后缀
$extension = pathinfo($filePath, PATHINFO_EXTENSION卡塔尔(قطر‎; //是不是要压缩
$needCompress = true; //判别财富文件是不是是以 .min.css只怕.min.js结尾的
//此类结尾常常都以已回降过,比方jquery.min.js,就没有必要再压缩了 if
(str_end_with($object, ‘.min.’.$extension, true卡塔尔(قطر‎卡塔尔 {
//压缩后的能源贮存路线,放在 /www/min/ 目录下 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔(英语:State of Qatar) – strlen($extension卡塔尔 – 4卡塔尔(قطر‎ .
$state .’.’. $extension; $needCompress = false; } else if
(in_array($extension, $this->compressResDir卡塔尔(英语:State of Qatar)卡塔尔(英语:State of Qatar) {
//此处是亟需减小的文件目录 $minObject = ‘min/’.substr($object, 0,
strlen($object卡塔尔国 – strlen($extension卡塔尔卡塔尔(قطر‎ . $state . ‘.’ . $extension;
//看看是不是是忽视的不二秘技前缀 foreach ($this->compressResIngorePrefix as
$v卡塔尔(قطر‎ { if (str_start_with($object, $v, true)) { $needCompress = false;
} } } else { $minObject = ‘min/’.$object; $needCompress = false; }
return true; } /** * 获取贮存能源版本的公文 * 它是位于多少个数组里 *
$resState = array( * ‘文件路线’ => ‘对应的版本号’, * ‘文件路线’
=> ‘对应的本子号’, * ‘文件路径’ => ‘对应的版本号’, * ); *
@return [type] [description] */ public function getResState() { if
(file_exists($this->resStatePath)) { require $this->resStatePath;
return $resState; } return []; } /** *
总括文件的版本号,那一个是基于测算文件MD5散列值获得版本号 *
只要文件内容退换了,所总括拿到的散列值就能够不平等 *
用于判定财富文件是不是有改观过 * @param [type] $filePath
[description] * @return [type] [description] */ public function
_getResStateVersion($filePath) { return
base_convert(crc32(md5_file($filePath)), 10, 36); } /** *
确定保障目录可写 * @param [type] $dir [description] * @return [type]
[description] */ private function _ensureWritableDir($dir) { if
(!file_exists($dir)) { @mkdir($dir, 0777, true); @chmod($dir, 0777); }
else if (!is_writable($dir)) { @chmod($dir, 0777); if
(!is_writable($dir)) { show_error(‘目录’.$dir.’不可写’); } } } /**
* 将削减后的能源文件写入到/www/min/下去 * @param [type] $filePath
[description] * @param [type] $minFilePath [description] *
@return [type] [description] */ private function
compressResFileAndSave($filePath, $minFilePath) { if
(!file_put_contents($minFilePath,
$this->compressResFile($filePath))) {
//$CI->exceptions->show_exception(“写入文件{$minFilePath}战败”卡塔尔(قطر‎;
show_error(“写入文件{$minFilePath}战败”, -1卡塔尔国; } } /** * 压缩财富文件
* @param [type] $filePath [description] * @return [type]
[description] */ private function compressResFile($filePath) {
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); if
($extension === ‘js’) { require_once ‘JShrink/Minifier.php’; return
\JShrink\Minifier::minify(file_get_contents($filePath)); } else if
($extension ===’css’) { $content = file_get_contents($filePath);
$content =
preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”,
$content); $content = str_replace([“\r\n”, “\r”, “\n”], ”,
$content); $content = preg_replace(‘/([{}),;:>])\s+/’, ‘$1’,
$content); $content = preg_replace(‘/\s+([{}),;:>])/’, ‘$1’,
$content); $content = str_replace(‘;}’, ‘}’, $content); return
$content; } else {
//$CI->exceptions->show_exception(“不支持压缩{extension}文件[$filePath]”);
show_error(“不支持压缩{extension}文件[$filePath]”, -1); } } private
function _saveResState($resState) { ksort($resState); $content =
“<?php\n\n\$resState = array(\n”; foreach ($resState as $k =>
$v) { $content .= “\t ‘$k’ => ‘$v’,\n”; } $content .= “);\n\n”;
file_put_contents($this->resStatePath, $content卡塔尔; } } 点击张开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
/**
* 资源压缩类
*/
class ResMinifier {
    /** 需要压缩的资源目录*/
    public $compressResDir = [‘css’, ‘js’];
    /** 忽略压缩的路径,例如此处是js/icon开头的路径忽略压缩*/
    public $compressResIngorePrefix = [‘js/icon’];
    /** 资源根目录*/
    public $resRootDir;
    /** 资源版本文件路径*/
    private $resStatePath;
 
    public function __construct() {
        $this->resRootDir = WEBROOT . ‘www/’;
        $this->resStatePath = WEBROOT . ‘www/resState.php’;
    }
 
    public function compressRes() {
        //获取存放版本的资源文件
        $resState = $this->getResState();
        $count = 0;
 
        //开始遍历需要压缩的资源目录
        foreach ($this->compressResDir as $resDir) {
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->resRootDir . $resDir , FilesystemIterator::SKIP_DOTS)) as $file) {
                //获取该资源文件的绝对路径
                $filePath = str_replace(‘\\’, ‘/’, $file->getRealPath());
 
                //获取文件相对路径
                $object = substr($filePath, strlen($this->resRootDir));
 
                //计算文件的版本号
                $state = $this->_getResStateVersion($filePath);
 
                //获取文件的几个参数值
                if (true !== $this->getObjectInfo($object, $minObject, $needCompress, $state, $extension)) {
                    continue;
                }
 
                //压缩文件的绝对路径
                $minFilePath = str_replace(‘\\’, ‘/’, $this->resRootDir. $minObject);
 
                //************此处p判断是最重要部分之一*****************//
                //判断文件是否存在且已经改动过
                if (isset($resState[$object]) && $resState[$object] == $state && isset($resState[$minObject]) && file_exists($minFilePath)) {
                    continue;
                }
 
                //确保/www/min/目录可写
                $this->_ensureWritableDir(dirname($minFilePath));
 
                if ($needCompress) {
                    $this->compressResFileAndSave($filePath, $minFilePath);
                } else {
                    copy($filePath, $minFilePath);
                }
 
                $resState[$object] = $state;
                $resState[$minObject] = ”;
                $count++;
 
                if ($count == 50) {
                    $this->_saveResState($resState);
                    $count = 0;
                }
 
            }
        }
        if($count) $this->_saveResState($resState);
    }
 
    /**
     * 获取资源文件相关信息
     * @param  [type] $object       资源文件路径 (www/css/home/index.css)
     * @param  [type] $minObject    压缩资源文件路径 (www/min/css/home/index.ae123a.css)
     * @param  [type] $needCompress 是否需要压缩
     * @param  [type] $state        文件版本号
     * @param  [type] $extension    文件名后缀
     * @return [type]               [description]
     */
    public function getObjectInfo($object, &$minObject, &$needCompress, &$state, &$extension) {
        //获取资源绝对路径
        $filePath = $this->resRootDir . $object;
        //判断资源是否存在
        if (!file_exists($filePath)) return "资源文件不存在{$filePath}";
        //版本号
        $state = $this-> _getResStateVersion($filePath);
        //文件名后缀
        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
        //是否要压缩
        $needCompress = true;
 
        //判断资源文件是否是以 .min.css或者.min.js结尾的
        //此类结尾一般都是已压缩过,例如jquery.min.js,就不必再压缩了
        if (str_end_with($object, ‘.min.’.$extension, true)) {
            //压缩后的资源存放路径,放在 /www/min/ 目录下
            $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension) – 4) . $state .’.’. $extension;
            $needCompress = false;
        } else if (in_array($extension, $this->compressResDir)) {
            //此处是需要压缩的文件目录
            $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension)) . $state . ‘.’ . $extension;
            //看看是否是忽略的路径前缀
            foreach ($this->compressResIngorePrefix as $v) {
                if (str_start_with($object, $v, true)) {
                    $needCompress = false;
                }
            }
        } else {
            $minObject = ‘min/’.$object;
            $needCompress = false;
        }
        return true;
    }
 
    /**
     * 获取存放资源版本的文件
     * 它是放在一个数组里
     * $resState = array(
     *         ‘文件路径’ => ‘对应的版本号’,
     *         ‘文件路径’ => ‘对应的版本号’,
     *         ‘文件路径’ => ‘对应的版本号’,
     *     );
     * @return [type] [description]
     */
    public function getResState() {
        if (file_exists($this->resStatePath)) {
            require $this->resStatePath;
            return $resState;
        }
        return [];
    }
 
    /**
     * 计算文件的版本号,这个是根据计算文件MD5散列值得到版本号
     * 只要文件内容改变了,所计算得到的散列值就会不一样
     * 用于判断资源文件是否有改动过
     * @param  [type] $filePath [description]
     * @return [type]           [description]
     */
    public function _getResStateVersion($filePath) {
        return base_convert(crc32(md5_file($filePath)), 10, 36);
    }
 
    /**
     * 确保目录可写
     * @param  [type] $dir [description]
     * @return [type]      [description]
     */
    private function _ensureWritableDir($dir) {
        if (!file_exists($dir)) {
            @mkdir($dir, 0777, true);
            @chmod($dir, 0777);
        } else if (!is_writable($dir)) {
            @chmod($dir, 0777);
            if (!is_writable($dir)) {
                show_error(‘目录’.$dir.’不可写’);
            }
        }
    }
 
    /**
     * 将压缩后的资源文件写入到/www/min/下去
     * @param  [type] $filePath    [description]
     * @param  [type] $minFilePath [description]
     * @return [type]              [description]
     */
    private function compressResFileAndSave($filePath, $minFilePath) {
        if (!file_put_contents($minFilePath, $this->compressResFile($filePath))) {
 
            //$CI->exceptions->show_exception("写入文件{$minFilePath}失败");
            show_error("写入文件{$minFilePath}失败", -1);
        }
    }
 
    /**
     * 压缩资源文件
     * @param  [type] $filePath [description]
     * @return [type]           [description]
     */
    private function compressResFile($filePath) {
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        if ($extension === ‘js’) {
            require_once ‘JShrink/Minifier.php’;
            return \JShrink\Minifier::minify(file_get_contents($filePath));
        } else if ($extension ===’css’) {
            $content = file_get_contents($filePath);
            $content = preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”, $content);
            $content = str_replace(["\r\n", "\r", "\n"], ”, $content);
            $content = preg_replace(‘/([{}),;:>])\s+/’, ‘$1’, $content);
            $content = preg_replace(‘/\s+([{}),;:>])/’, ‘$1’, $content);
            $content = str_replace(‘;}’, ‘}’, $content);
            return $content;
        } else {
            //$CI->exceptions->show_exception("不支持压缩{extension}文件[$filePath]");
            show_error("不支持压缩{extension}文件[$filePath]", -1);
 
        }
    }
 
    private function _saveResState($resState) {
        ksort($resState);
        $content = "<?php\n\n\$resState = array(\n";
        foreach ($resState as $k => $v) {
            $content .= "\t ‘$k’ => ‘$v’,\n";
        }
        $content .= ");\n\n";
        file_put_contents($this->resStatePath, $content);
    }
 
}
 
点击打开

Common.php

PHP

<?php /** * 输出 HttpHead 中的能源总是。 css/js 自动剖断真实路线
* @param string 文件路线 * @return string */ function
res_link($file) { $file = res_path($file, $extension); if ($extension
=== ‘css’) { return ‘<link rel=”stylesheet” type=”text/css” href=”‘ .
$file . ‘”/>’; } else if ($extension === ‘js’) { return ‘<script
type=”text/javascript” src=”‘.$file.'”></script>’; } else {
return false; } } /** * 智能路由财富实际路线 * @param string 路径 *
@param string 扩展名 * @return string 真实路线 */ function
res_path($file, &$extension卡塔尔 { //检查是否存在询问字符串 list($file,
$query卡塔尔国 = explode(‘?’, $file . ‘?’卡塔尔国; //得到扩张名 $extension =
strtolower(pathinfo($file, PATHINFO_EXTENSION)); // $file =
str_replace(‘\\’, ‘/’, $file); //取稳妥前调节器名 global $class; if
($class == null卡塔尔 exit(‘can not get class name’卡塔尔; $className =
strtolower($class卡塔尔国; //此处的平整是如此: //举个例子,假设不加 /
,Home调节器对应的格式是: index.css,那么
此处的路线会产生css/home/index.css //借使有 / ,调整器的格式能够是
/main.css,那么这里的路线会形成 css/main.css(公用的css类) if (‘/’ !==
$file[0]卡塔尔(قطر‎ { //index.css => css/home/index.css $object = $extension
.’/’. $className .’/’ . $file; } else { // /css/main.css 只怕 /main.css
=> css/main.css $object = substr($file, 1卡塔尔(قطر‎; //若object是 main.css
,则自动抬高 扩展名目录 => css/main.css if (0 !==
strncasecmp($extension, $object, strlen($extension卡塔尔(英语:State of Qatar)卡塔尔(قطر‎卡塔尔国 { $object =
$extension . ‘/’ . $object; } } //财富真实路线 $filepath =
WEBROOT.’www/’.$object; //替换压缩版本,那部分逻辑与公事减少逻辑对应 if
(in_array($extension, array(‘css’, ‘js’))) {
if(!str_start_with($object, ‘min/’) &&
file_exists(APPPATH.’libraries/ResMinifier.php’)) { require_once
应用程式PATH.’libraries/Res迷你fier.php’; $resminifier = new ResMinifier(卡塔尔国;
//获取贮存能源版本的文书的数组变量 $resState =
$resminifier->getResState(卡塔尔(英语:State of Qatar); //计算取妥贴前文件版本号 $state =
$resminifier->_getResStateVersion($filepath卡塔尔; //判别该版本号是还是不是留存
if (isset($resState[$object]卡塔尔(قطر‎卡塔尔 { //剖断是还是不是是.min.css或.min.js结尾 if
(str_end_with($object, ‘.min.’.$extension卡塔尔(英语:State of Qatar)卡塔尔 {
//将版本号拼接上去,然后拿走min的文件路线 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔(英语:State of Qatar) – strlen($extension卡塔尔国 – 4卡塔尔(英语:State of Qatar) .
$state . ‘.’ . $extension; } else {
//将版本号拼接上去,然后拿走min的文本路线 $minObject =
‘min/’.substr($object, 0, strlen($object卡塔尔 – strlen($extension卡塔尔(قطر‎卡塔尔国 . $state
. ‘.’ . $extension; } //推断min的渠道是还是不是存在在$resState里面 if
(isset($resState[$minObject])) { $object = $minObject; $query = ”; }
} } $file = RES_BASE_URL . $object; } return ($query == null) ? $file
: ($file .’?’. $query); } /** * 推断 subject 是或不是以 search带头,
参数钦点是还是不是忽视大小写 * @param [type] $subject [description] *
@param [type] $search [description] * @param boolean $ignore_case
[description] * @return [type] [description] */ function
str_start_with($subject, $search, $ignore_case = false) { $len2 =
strlen($search); if (0 === $len2) return true; $len1 = strlen($subject);
if ($len1 < $len2) return false; if ($ignore_case) { return 0 ===
strncmp($subject, $search, $len2); } else { return 0 ===
strncasecmp($subject, $search, $len2); } } /** * 推断 subject 是或不是以
search结尾, 参数钦点是还是不是忽视大小写 * @param [type] $subject
[description] * @param [type] $search [description] * @param
boolean $ignore_case [description] * @return [type]
[description] */ function str_end_with($subject, $search,
$ignore_case = false) { $len2 = strlen($search); if (0 === $len2)
return true; $len1 = strlen($subject); if ($len2 > $len1) return
false; if ($ignore_case卡塔尔国 { return 0 === strcmp(substr($subject, $len1 –
$len2卡塔尔(英语:State of Qatar), $search卡塔尔(قطر‎; } else { return 0 === strcasecmp(substr($subject,
$len1 – $len2卡塔尔(英语:State of Qatar), $search卡塔尔(قطر‎; } } 点击展开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
    /**
     * 输出 HttpHead 中的资源连接。 css/js 自动判断真实路径
     * @param  string  文件路径
     * @return string      
     */
    function res_link($file) {
        $file = res_path($file, $extension);
 
        if ($extension === ‘css’) {
           return ‘<link rel="stylesheet" type="text/css" href="’ . $file . ‘"/>’;
        } else if ($extension === ‘js’) {
            return ‘<script type="text/javascript" src="’.$file.’"></script>’;
        } else {
            return false;
        }
    }
 
    /**
     * 智能路由资源真实路径
     * @param  string      路径
     * @param  string      扩展名
     * @return string       真实路径
     */
    function res_path($file, &$extension) {
        //检查是否存在查询字符串
        list($file, $query) = explode(‘?’, $file . ‘?’);
        //取得扩展名
        $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        //
        $file = str_replace(‘\\’, ‘/’, $file);
        //取得当前控制器名
        global $class;
        if ($class == null) exit(‘can not get class name’);
        $className = strtolower($class);
 
        //此处的规则是这样:
        //例如,如果不加 / ,Home控制器对应的格式是: index.css,那么 此处的路径会变成css/home/index.css
        //假如有 / ,控制器的格式可以是 /main.css,那么此处的路径会变成 css/main.css(公用的css类)
        if (‘/’ !== $file[0]) {
            //index.css => css/home/index.css
            $object = $extension .’/’. $className .’/’ . $file;
        } else {
            // /css/main.css 或者 /main.css => css/main.css
            $object = substr($file, 1);
 
            //若object是 main.css ,则自动加上 扩展名目录 => css/main.css
            if (0 !== strncasecmp($extension, $object, strlen($extension))) {
                $object = $extension . ‘/’ . $object;
            }
        }
        //资源真实路径
        $filepath = WEBROOT.’www/’.$object;
 
        //替换压缩版本,这部分逻辑与文件压缩逻辑对应
        if (in_array($extension, array(‘css’, ‘js’))) {
            if(!str_start_with($object, ‘min/’) && file_exists(APPPATH.’libraries/ResMinifier.php’)) {
                require_once APPPATH.’libraries/ResMinifier.php’;
                $resminifier = new ResMinifier();
                //获取存放资源版本的文件的数组变量
                $resState = $resminifier->getResState();
                //计算得到当前文件版本号
                $state = $resminifier->_getResStateVersion($filepath);
                //判断该版本号是否存在
                if (isset($resState[$object])) {
                    //判断是否是.min.css或.min.js结尾
                    if (str_end_with($object, ‘.min.’.$extension)) {
                        //将版本号拼接上去,然后得到min的文件路径
                        $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension) – 4) . $state . ‘.’ . $extension;
                    } else {
                        //将版本号拼接上去,然后得到min的文件路径
                        $minObject = ‘min/’.substr($object, 0, strlen($object) – strlen($extension)) . $state . ‘.’ . $extension;
                    }
                    //判断min的路径是否存在在$resState里面
                     if (isset($resState[$minObject])) {
                        $object = $minObject;
                        $query = ”;
                     }
                }
 
            }
 
            $file = RES_BASE_URL . $object;
        }
 
        return ($query == null) ? $file : ($file .’?’. $query);
 
    }
 
    /**
     * 判断 subject 是否以 search开头, 参数指定是否忽略大小写
     * @param  [type]  $subject     [description]
     * @param  [type]  $search      [description]
     * @param  boolean $ignore_case [description]
     * @return [type]               [description]
     */
    function str_start_with($subject, $search, $ignore_case = false) {
        $len2 = strlen($search);
        if (0 === $len2) return true;
        $len1 = strlen($subject);
        if ($len1 < $len2) return false;
        if ($ignore_case) {
            return 0 === strncmp($subject, $search, $len2);
        } else {
            return 0 === strncasecmp($subject, $search, $len2);
        }
    }
 
    /**
     * 判断 subject 是否以 search结尾, 参数指定是否忽略大小写
     * @param  [type]  $subject     [description]
     * @param  [type]  $search      [description]
     * @param  boolean $ignore_case [description]
     * @return [type]               [description]
     */
    function str_end_with($subject, $search, $ignore_case = false) {
        $len2 = strlen($search);
        if (0 === $len2) return true;
        $len1 = strlen($subject);
        if ($len2 > $len1) return false;
        if ($ignore_case) {
            return 0 === strcmp(substr($subject, $len1 – $len2), $search);
        } else {
            return 0 === strcasecmp(substr($subject, $len1 – $len2), $search);
        }
    }
 
点击打开

$resState.php(里面包车型客车代码是自动生成的卡塔尔(قطر‎

XHTML

<?php $resState = array( ‘css/home/index.css’ => ‘gwy933’,
‘js/echarts-all.min.js’ => ‘wqrf1c’, ‘js/home/index.js’ =>
‘s2z6f5’, ‘js/icon.js’ => ‘pgcyih’, ‘js/icon_home.js’ =>
‘zhl9iu’, ‘js/ion.rangeSlider.min.js’ => ‘akq381’,
‘js/jquery-ui-autocomplete.js’ => ‘8nzacv’, ‘js/jquery-ui.min.js’
=> ‘i6tw8z’, ‘js/jquery.all.min.js’ => ‘d2w76v’,
‘js/jquery.city.js’ => ‘toxdrf’, ‘js/jquery.easydropdown.min.js’
=> ‘2ni3i0’, ‘js/jquery.matrix.js’ => ‘3vrqkk’,
‘js/jquery.mobile.all.min.js’ => ‘ernu7r’, ‘js/jquery.qrcode.min.js’
=> ‘yuhnsj’, ‘js/jquery.tinyscrollbar.min.js’ => ‘oakk3c’,
‘js/mobiscroll.custom.min.js’ => ‘kn8h2e’, ‘js/store.min.js’ =>
‘n50jwr’, ‘js/swiper.animate1.0.2.min.js’ => ‘mm27zc’,
‘js/swiper.min.js’ => ‘jicwhh’, ‘min/css/home/index.6a4e83eb.css’
=> ”, ‘min/css/home/index.gwy933.css’ => ”,
‘min/css/home/index.puzbnf.css’ => ”,
‘min/css/home/index.thv8x7.css’ => ”,
‘min/js/echarts-all.76025ee0.js’ => ”,
‘min/js/echarts-all.wqrf1c.js’ => ”, ‘min/js/home/index.65363d41.js’
=> ”, ‘min/js/home/index.s2z6f5.js’ => ”,
‘min/js/icon.5bbd4db9.js’ => ”, ‘min/js/icon.pgcyih.js’ => ”,
‘min/js/icon_home.7fe74076.js’ => ”, ‘min/js/icon_home.zhl9iu.js’
=> ”, ‘min/js/ion.rangeSlider.261d8ed1.js’ => ”,
‘min/js/ion.rangeSlider.akq381.js’ => ”,
‘min/js/jquery-ui-autocomplete.1f3bb62f.js’ => ”,
‘min/js/jquery-ui-autocomplete.8nzacv.js’ => ”,
‘min/js/jquery-ui.418e9683.js’ => ”, ‘min/js/jquery-ui.i6tw8z.js’
=> ”, ‘min/js/jquery.all.2f248267.js’ => ”,
‘min/js/jquery.all.d2w76v.js’ => ”, ‘min/js/jquery.city.6b036feb.js’
=> ”, ‘min/js/jquery.city.toxdrf.js’ => ”,
‘min/js/jquery.easydropdown.2ni3i0.js’ => ”,
‘min/js/jquery.easydropdown.98fa138.js’ => ”,
‘min/js/jquery.matrix.3vrqkk.js’ => ”,
‘min/js/jquery.matrix.dfe2a44.js’ => ”,
‘min/js/jquery.mobile.all.3539ebb7.js’ => ”,
‘min/js/jquery.mobile.all.ernu7r.js’ => ”,
‘min/js/jquery.qrcode.7d9738b3.js’ => ”,
‘min/js/jquery.qrcode.yuhnsj.js’ => ”,
‘min/js/jquery.tinyscrollbar.578e4cb8.js’ => ”,
‘min/js/jquery.tinyscrollbar.oakk3c.js’ => ”,
‘min/js/mobiscroll.custom.4a684f66.js’ => ”,
‘min/js/mobiscroll.custom.kn8h2e.js’ => ”,
‘min/js/store.536545cb.js’ => ”, ‘min/js/store.n50jwr.js’ => ”,
‘min/js/swiper.4650ad75.js’ => ”,
‘min/js/swiper.animate1.0.2.517f82e8.js’ => ”,
‘min/js/swiper.animate1.0.2.mm27zc.js’ => ”,
‘min/js/swiper.jicwhh.js’ => ”, 卡塔尔; 点击张开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
 
$resState = array(
     ‘css/home/index.css’ => ‘gwy933’,
     ‘js/echarts-all.min.js’ => ‘wqrf1c’,
     ‘js/home/index.js’ => ‘s2z6f5’,
     ‘js/icon.js’ => ‘pgcyih’,
     ‘js/icon_home.js’ => ‘zhl9iu’,
     ‘js/ion.rangeSlider.min.js’ => ‘akq381’,
     ‘js/jquery-ui-autocomplete.js’ => ‘8nzacv’,
     ‘js/jquery-ui.min.js’ => ‘i6tw8z’,
     ‘js/jquery.all.min.js’ => ‘d2w76v’,
     ‘js/jquery.city.js’ => ‘toxdrf’,
     ‘js/jquery.easydropdown.min.js’ => ‘2ni3i0’,
     ‘js/jquery.matrix.js’ => ‘3vrqkk’,
     ‘js/jquery.mobile.all.min.js’ => ‘ernu7r’,
     ‘js/jquery.qrcode.min.js’ => ‘yuhnsj’,
     ‘js/jquery.tinyscrollbar.min.js’ => ‘oakk3c’,
     ‘js/mobiscroll.custom.min.js’ => ‘kn8h2e’,
     ‘js/store.min.js’ => ‘n50jwr’,
     ‘js/swiper.animate1.0.2.min.js’ => ‘mm27zc’,
     ‘js/swiper.min.js’ => ‘jicwhh’,
     ‘min/css/home/index.6a4e83eb.css’ => ”,
     ‘min/css/home/index.gwy933.css’ => ”,
     ‘min/css/home/index.puzbnf.css’ => ”,
     ‘min/css/home/index.thv8x7.css’ => ”,
     ‘min/js/echarts-all.76025ee0.js’ => ”,
     ‘min/js/echarts-all.wqrf1c.js’ => ”,
     ‘min/js/home/index.65363d41.js’ => ”,
     ‘min/js/home/index.s2z6f5.js’ => ”,
     ‘min/js/icon.5bbd4db9.js’ => ”,
     ‘min/js/icon.pgcyih.js’ => ”,
     ‘min/js/icon_home.7fe74076.js’ => ”,
     ‘min/js/icon_home.zhl9iu.js’ => ”,
     ‘min/js/ion.rangeSlider.261d8ed1.js’ => ”,
     ‘min/js/ion.rangeSlider.akq381.js’ => ”,
     ‘min/js/jquery-ui-autocomplete.1f3bb62f.js’ => ”,
     ‘min/js/jquery-ui-autocomplete.8nzacv.js’ => ”,
     ‘min/js/jquery-ui.418e9683.js’ => ”,
     ‘min/js/jquery-ui.i6tw8z.js’ => ”,
     ‘min/js/jquery.all.2f248267.js’ => ”,
     ‘min/js/jquery.all.d2w76v.js’ => ”,
     ‘min/js/jquery.city.6b036feb.js’ => ”,
     ‘min/js/jquery.city.toxdrf.js’ => ”,
     ‘min/js/jquery.easydropdown.2ni3i0.js’ => ”,
     ‘min/js/jquery.easydropdown.98fa138.js’ => ”,
     ‘min/js/jquery.matrix.3vrqkk.js’ => ”,
     ‘min/js/jquery.matrix.dfe2a44.js’ => ”,
     ‘min/js/jquery.mobile.all.3539ebb7.js’ => ”,
     ‘min/js/jquery.mobile.all.ernu7r.js’ => ”,
     ‘min/js/jquery.qrcode.7d9738b3.js’ => ”,
     ‘min/js/jquery.qrcode.yuhnsj.js’ => ”,
     ‘min/js/jquery.tinyscrollbar.578e4cb8.js’ => ”,
     ‘min/js/jquery.tinyscrollbar.oakk3c.js’ => ”,
     ‘min/js/mobiscroll.custom.4a684f66.js’ => ”,
     ‘min/js/mobiscroll.custom.kn8h2e.js’ => ”,
     ‘min/js/store.536545cb.js’ => ”,
     ‘min/js/store.n50jwr.js’ => ”,
     ‘min/js/swiper.4650ad75.js’ => ”,
     ‘min/js/swiper.animate1.0.2.517f82e8.js’ => ”,
     ‘min/js/swiper.animate1.0.2.mm27zc.js’ => ”,
     ‘min/js/swiper.jicwhh.js’ => ”,
);
 
点击打开

 

除此以外附上JShrink这么些PHP类的链接给我们下载 

万风流倜傥大家依然认为远远不够OK的话,笔者一向将以此实验项目打包供大家下载下来学习和询问:

四、结语

谈到底自个儿来享受大家线上类其他现实性得以完毕方案:

咱俩的项目分线上情状、开采意况和测量试验情况,在支付和测验景况中,我们每趟访问都会调用压缩文件的接口,然后再对转移的能源文件的轻重是要做决断的,要是减少后文件过小,就要求将该能源文件的代码合併到别的能源文件里去,以此降低不需求的HTTP央浼(因为文件太小,财富的下载时间远小于HTTP央求响应所消耗的日子);另三个是图片的拍卖,全体图片都要经过压缩技艺经过(举例在: 
那些网站去收缩图片),在PC端,要是是小Logo的话,使用图片合併的法子开展优化,实际情况可参照他事他说加以考察本身的那篇博文:http://www.cnblogs.com/it-cen/p/4618954.html   
而在wap端的图片处理利用的是base64编码情势来管理图片,详细情形可以参照自身的那篇博文: 
,当页面输出时,会接收redis来缓存页面(为什么用内存来缓存并不是运用页面缓存,这些未来再享受给大家)。假设是线上情形,每发叁回版本,才会调用一下能源文件收缩那么些接口,并且线上的静态财富(css、js、图片)是寄放在在Ali云的OSS里的,与大家的应用服务器是分开的。那是大家线上类别的后生可畏都部队分优化建设方案,当然了,还大概有越来越多优化技术,笔者会在现在各类总计和享用出去,方便大家一起读书和调换。

此番博文就享受到此,多谢观察此博文的对象们。

1 赞 1 收藏
评论

澳门葡京 7

文本上传例子

class html
{
var $dir; //dir for the htmls(without/)
var $rootdir; //root of html files(without/):html
var $name; //html文件存放路线
var $dirname; //钦定的文书夹名称
var $url; //获取html文件消息的起点网页地址
var $time; //html文件消息填加时的时辰
var $dirtype; //目录寄放方式:year,month,,,,
var $nametype; //html文件命名方式:name

<?php
/**
* 邮件类
* Enter description here …
* @author df
* Mail::getMail()->sendMail();
*
*/
class Mail{

 代码如下

function html($nametype=’name’,$dirtype=’year’,$rootdir=’html’)

private static $__mail=null;//邮箱类对象
private $__PMAIL=NULL;
function __construct(){
require(‘class.phpmailer.php’);
$this->__PMAIL=new PHPMailer();
$this->__PMAIL->IsSMTP();
$this->__PMAIL->Host= “smtp.163.com”;
$this->__PMAIL->Username=””;
$this->__PMAIL->Password=””;
$this->__PMAIL->SMTPAuth=true;
$this->__PMAIL->From=””;
}
public static function getMail(){
/*if(!self::$__mail){
self::$__mail=new Mail();
}
return self::$__mail;
*/
return new Mail();
}
/**
*
* Enter description here …
* @param unknown_type $toAddress //收件人地址,多少个可用,好分隔
* @param unknown_type $fromName //发件人名字
* @param unknown_type $title //标题
* @param unknown_type $content //内容
* @param unknown_type $isHTML //是不是以HTML格式发送
* @param unknown_type $charset //编码
* @param unknown_type $replyto //收件人地址
* @param unknown_type $attachment //附件
*/
function
sendMail($toAddress,$title=”,$content=”,$isHTML=TRUE,$charset=’utf-8′,$replyto=”,$fromName=”,$attachment=”){
$this->__PMAIL->IsHTML($isHTML);
$this->__PMAIL->CharSet=$charset;
if(strstr($toAddress,’,’)){
$__list=explode(‘,’, $toAddress);
foreach ($__list as $v){
$this->__PMAIL->AddAddress($v,$v);
}
}else{
$this->__PMAIL->AddAddress($toAddress,$toAddress);
}

<?php
header(‘content-type:text/html;charset=utf-8’);
require(‘uploadFile.php’);

function setvar($nametype=’name’,$dirtype=’year’,$rootdir=’html’)

$this->__PMAIL->FromName=$fromName;
$this->__PMAIL->Subject=$title;
$this->__PMAIL->Body=$content;
if($replyto){
$this->__PMAIL->AddReplyTo($replyto);
}
if($attachment){
$this->__PMAIL->AddAttachment($attachment,$attachment);
}
return $this->__PMAIL->Send();
}
}

if(isset($_POST[‘submit’])){
$uploads = $_FILES[‘file’];
$num_file = count($uploads[‘name’]);

function createdir($dir=”)
{
$this->dir=$dir?$dir:$this->dir;

$up = new UploadFile($uploads,’uploads’,1024);
$num = $up->upload();

if (!is_dir($this->dir))
{
$temp = explode(‘/’,$this->dir);
$cur_dir = ”;
for($i=0;$i {
$cur_dir .= $temp[$i].’/’;
if (!is_dir($cur_dir))

if($num == $num_file ){
echo ‘全部文件上传成功’;
exit;
}else{
echo $num,’个文件上传成功<br/>’;
echo $up->showErrorInfo();
exit;
}
}

}
}
}

?>

function getdir($dirname=”,$time=0)
{
$this->time=$time?$time:$this->time;
$this->dirname=$dirname?$dirname:$this->dirname;

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;
<html xmlns=” 1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″
/>
<title>无标题文书档案</title>
</head>

switch($this->dirtype)
{
case ‘name’:
if(empty($this->dirname))
$this->dir=$this->rootdir;
else
$this->dir=$this->rootdir.’/’.$this->dirname;
break;
case ‘year’:
$this->dir=$this->rootdir.’/’.date(“Y”,$this->time);
break;

<body>
<form action=”uup.php” method=”post”
enctype=”multipart/form-data”>
<p><input name=”file[]” type=”file” /></p>
<p><input name=”file[]” type=”file” /></p>
<p><input name=”file[]” type=”file” /></p>
<p><input name=”file[]” type=”file” /></p>
<input name=”submit” type=”submit” />
</form>
</body>
</html>

case ‘month’:
$this->dir=$this->rootdir.’/’.date(“Y-m”,$this->time);
break;

文本上传类代码

case ‘day’:
$this->dir=$this->rootdir.’/’.date(“Y-m-d”,$this->time);
break;
}

 代码如下

$this->createdir();

<?php
/*————*/
class UploadFile
{
var $user_post_file  = array();
var $save_file_path  = ”;
var $max_file_size   = ”;
var $allow_type      =
array(‘gif’,’jpg’,’png’,’zip’,’rar’,’txt’,’doc’,’pdf’);
var $final_file_path = ”;
var $save_info       = array();
var $error_info      = array();

return $this->dir;
}

/**
*结构函数,用于起始化音信。
*
*@param Array $file
*@param String $path
*@param Integer $size
*@param Array $type
*/
function __construct($file,$path,$size = 2097152,$type=”)
{
$this->user_post_file   = $file;
$this->save_file_path   = $path;
$this->max_file_size    = $size;
if(!$type=”){
$this->allow_type[] = $type;
}
}

function geturlname($url=”)
{
$this->url=$url?$url:$this->url;

/**
*
*
*@access public
*@return int
*/
function upload()
{
for($i=0;$i<count($this->user_post_file[‘name’]);$i++)
{
if($this->user_post_file[‘error’][$i] ==
0卡塔尔(قطر‎{//上传文件状态符合规律
//获取当前文件名,有时文件名,大小,类型,增加名
$name     = $this->user_post_file[‘name’][$i];
$tmp_name = $this->user_post_file[‘tmp_name’][$i];
$size     = $this->user_post_file[‘size’][$i];
$type     = $this->user_post_file[‘type’][$i];
$ext_name = $this->getExtName($name);

$filename=basename($this->url);
$filename=explode(“.”,$filename);
return $filename[0];
}

//文件大小
if(!$this->checkSize($size)){
$this->error_info[] = ‘您上传的文书:’.$name.’太大’;
continue;
}
//扩展名
if(!$this->checkType($ext_name)){
$this->error_info[] = ‘您上传的文件:’.$name.’不合规’;
continue;
}
//违规上传
if(!is_uploaded_file($tmp_name)){
$this->error_info[] = ‘您上传的文书:’.$name.’归属违规提交’;
continue;
}

case ‘nametime’:
$this->name=$this->dir.’/’.$this->geturlname().’-‘.$this->time.’.htm’;
break;

//
$basename = $this->getBaseName($name,”.”.$ext_name);
$final_filename =
$basename.’-‘.time().’-‘.rand(1,10000).’.’.$ext_name;
$this->final_file_path =
$this->save_file_path.’/’.$final_filename;

}
return $this->name;
}

if(!move_uploaded_file($tmp_name,$this->final_file_path)){
$this->error_info = $this->user_post_file[‘error’][$i];
continue;
}

function createhtml($url=”,$time=0,$dirname=”,$htmlname=”)
{
$this->url=$url?$url:$this->url;
$this->dirname=$dirname?$dirname:$this->dirname;
$this->time=$time?$time:$this->time;
//上边保险不重复地把变量授予该类成员
if(empty($htmlname))
$this->getname();
else
$this->name=$dirname.’/’.$htmlname; //得到name

//
$this->save_info[] = array(
“name” => $name,
“ext_name” => $ext_name,
          “type” => $type,
                            “size” => $size,
“final_filename” => $final_filename,
                            “path” => $this->final_file_path
);
}
}

$content=file($this->url) or die(“Failed to open the url
“.$this->url.” !”);;

return count($this->save_info);
}

///////////////关键步—用file读取$this->url

/*
 *检查客户上传文件的轻重时候合法
 *
 *@param Integer $size
 *@access private
 *@return boolean
 */
function checkSize($size)
{
if($size > $this->max_file_size){
return FALSE;
}

$content=join(“”,$content);
$fp=@fopen($this-%3Ename,%22w“) or die(“Failed to open the file
“.$this->name.” !”);
if(@fwrite($fp,$content))
return true;
else
return false;
fclose($fp);
}
/////////////////以name为名字生成html

return TRUE;
}

function deletehtml($url=”,$time=0,$dirname=”)
{
$this->url=$url?$url:$this->url;
$this->time=$time?$time:$this->time;

/*
 *检查客商上传文件的花色是不是合法
 *
 *@access private
 *@return boolean
 */
function checkType($extension)
{
foreach($this->allow_type
as $type){
if(strcasecmp($extension,$type) == 0){
return TRUE;
}
}

$this->getname();

return FALSE;
}

if(@unlink($this->name))
return true;
else
return false;
}

/*
 *赢得文件的扩张名
 *
 *@param string $filename
 *@access private
 *@return string
 */
function getExtName($filename)
{
$p = pathinfo($filename);

/**
* function::deletedir()
* 删除目录
* @param $file 目录名(不带/)
* @return 
*/
function deletedir($file)
{
if(file_exists($file))
{
if(is_dir($file))
{
$handle =opendir($file);
while(false!==($filename=readdir($handle)))

return $p[‘extension’];
}

closedir($handle);
rmdir($file);
return true;
}else
}
}

/*
 *获得文件名(不包罗扩充名)
 *
 *@param string $filename
 *@param string $type
 *@access private
 *@return boolean
 */
function getBaseName($filename,$ext_name)
{
$basename = basename($filename,$ext_name);

}
?>

return $basename;
}

/*
 *
 *
 *
 */
function showErrorInfo()
{
if(count($this->error_info) != 0){
//echo ‘error…<br/>’;
foreach($this->error_info as $k=>$v){
echo ($k+1),’:’,$v,'<br/>’;
}
}
}

function getSaveInfo()
{
return $this->save_info;
}
}

//$upload = new UploadFile(”,”);
//$upload = new UploadFile();
//$upload->showErrorInfo();

?>

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website