Configure PHP.ini
Change configuration item (must) Auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php"
Change configuration item (optional) Allow_url_include = On
auto_prepend_file.php File Contents
Copy Code code as follows:
<?php
/**
* Introduce static file
* @param {array|string} relative path
* @param {string} The path where the script is currently executing __file__
*
*/
function import_static ($files, $path =null) {
Change the execution path of the current script
$old _dir = GETCWD ();
$tmp _dir = (isset ($path))? DirName ($path): DirName (__file__);
ChDir ($tmp _dir);
Organize include files
if (!is_array ($files)) {
$tmp = Array ();
$tmp [] = $files;
$files = $tmp;
}
Send header information
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);
}
}
Introducing Include files
foreach ($files as $key => $value) {
Require_once ($value);
}
Change back to the execution path of the current script
ChDir ($old _dir);
}
?>
How to use
Copy Code code as follows:
"A.js", "B.js" and "... /c.js "is the JS file to be merged, merge it into base.js.php, then the code in base.js.php is as follows:
<?php
Import_static Array (
' A.js ',
' B.js ',
'.. /c.js ',
'.. /moduleb/all.js.php '//can also refer to. php file
), __file__);
?>
The <script type= "Text/javascript" src= "base.js.php" ></script> can be introduced in HTML pages.
Before the product on-line, uses the batch processing file to carry on the processing, mainly does two aspects the work
1. Output "*.js.php" to the "*.js" file and delete "*.js.php". Command line: PHP *.js.php > *.js
2. Replace the reference to "*.js.php" with "*.js" in the HTML page. Preg_replace ()
The Ps:import_static function solves the problem of the include () process relative path in PHP.
The above is the entire content of this article, more detailed information please look forward to the following article