PHP头条
热点:

PHP合并静态文件


配置PHP.ini
更改配置项(必须)auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php"
更改配置项(可选)allow_url_include = On

auto_prepend_file.php文件内容

1. <?php  
2. /** 
3.  * 引入static文件 
4.  * @param {array|string} 相对路径 
5.  * @param {string} 当前执行脚本所在的路径__FILE__ 
6.  * 
7.  */ 
8. function import_static($files, $path=NULL){  
9.     // 更改当前脚本的执行路径  
10.     $old_dir = getcwd();  
11.     $tmp_dir = (isset($path)) ? dirname($path): dirname(__FILE__);  
12.     chdir($tmp_dir);  
13.     // 整理包含文件  
14.     if (!is_array($files)) {  
15.         $tmp = array();  
16.         $tmp[] = $files;  
17.         $files = $tmp;  
18.     }  
19.     // 发送头信息  
20.     if (isset($files[0])) {  
21.         if (stripos($files[0], '.js') !== false) {  
22.             $header_str = 'Content-Type:   text/javascript';  
23.         } elseif (stripos($files[0], '.css') !== false) {  
24.             $header_str = 'Content-Type:   text/css';  
25.         }  
26.         if (!ob_get_contents()) {  
27.             header($header_str);  
28.         }  
29.     }  
30.     // 引入包含文件 www.2cto.com   
31.     foreach($files as $key=>$value) {  
32.         require_once($value);  
33.     }  
34.     // 改回当前脚本的执行路径  
35.     chdir($old_dir);  
36. }  
37. ?> 

使用方法
"a.js"、"b.js"和"../c.js"是待合并的JS文件,将其合并为base.js.php,则base.js.php中的代码如下:

1. <?php  
2.     import_static(array(  
3.         'a.js',  
4.         'b.js',  
5.         '../c.js',  
6.         '../moduleB/all.js.php'    // 也可引用.php文件  
7.     ), __FILE__);  
8. ?> 
在HTML页面中使用<script type="text/javascript" src="base.js.php"></script>即可引入。
产品上线前,使用批处理文件进行处理,主要做两方面的工作
1. 将"*.js.php"输出到"*.js"文件,并删除"*.js.php"。命令行:php *.js.php &gt *.js
2. 将HTML页面中对"*.js.php"的引用替换为"*.js"。preg_replace()
PS:import_static函数解决了PHP中include()处理相对路径的问题。
待续。。。


摘自 Rain Man

www.phpzy.comtrue/phprm/17193.htmlTechArticlePHP合并静态文件 配置PHP.ini 更改配置项(必须)auto_prepend_file = C:\xampp\htdocs\auto_prepend_file.php 更改配置项(可选)allow_url_include = On auto_prepend_file.php文件内容 1.?php 2./** 3. * 引入static文件...

相关文章

    暂无相关文章

PHP之友评论

今天推荐