Configuration PHP.ini
Change configuration items (must) auto_prepend_file = "C: \ xampp \ htdocs \ auto_prepend_file.php"
Change configuration items (optional) allow_url_include = On
auto_prepend_file.php file contents
<?php
/**
* 引入static文件
* @param {array|string} 相对路径
* @param {string} 当前执行脚本所在的路径__FILE__
*
*/
function import_static($files, $path=NULL){
// 更改当前脚本的执行路径
$old_dir = getcwd();
$tmp_dir = (isset($path)) ? dirname($path): dirname(__FILE__);
chdir($tmp_dir);
// 整理包含文件
if (!is_array($files)) {
$tmp = array();
$tmp[] = $files;
$files = $tmp;
}
// 发送头信息
if (isset($files[0])) {
if (stripos($files[0], '.js') !== false) {
$header_str = 'Content-Type: text/javascript';
} elseif (stripos($files[0], '.css') !== false) {
$header_str = 'Content-Type: text/css';
}
if (!ob_get_contents()) {
header($header_str);
}
}
// 引入包含文件
foreach($files as $key=>$value) {
require_once($value);
}
// 改回当前脚本的执行路径
chdir($old_dir);
}
?>
Instructions
"A.js", "b.js" and the JS file "../c.js" is to be merged, will be combined into base.js.php, base.js.php in the code as follows:
<?php
import_static(array(
'a.js',
'b.js',
'../c.js',
'../moduleB/all.js.php' // 也可引用.php文件
), __FILE__);
?>
Using <script type = "text / javascript" src = "base.js.php"> </ script> in HTML pages can be introduced.
Before the product online, use a batch file processing, mainly to do the work of two
- The "* .js.php" output to the "* .js" file and delete the "* .js.php". Command line: php * .js.php & gt * .js
- The HTML page of "* .js.php" replace references to "* .js". preg_replace ()
PS: import_static in PHP function to solve the include () deal with issues relative path.
to be continued. . .
Reproduced in: https: //www.cnblogs.com/rainman/archive/2012/04/28/2474451.html