Home  >  Article  >  Backend Development  >  Detailed explanation of merging static files in PHP, _PHP tutorial

Detailed explanation of merging static files in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:09728browse

Detailed explanation of merging static files in PHP,

Configure PHP.ini

Change configuration item (required) auto_prepend_file = "C:xampphtdocsauto_prepend_file.php"

Change configuration item (optional) allow_url_include = On

auto_prepend_file.php file content

Copy code The code is as follows:

/**
*Introduce static files
* @param {array|string} relative path
* @param {string} The path where the currently executed script is located __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 included 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);
}
}
//Introduce include files
foreach($files as $key=>$value) {
         require_once($value);
}
//Change back the execution path of the current script
chdir($old_dir);
}
?>

How to use

Copy code The code is as follows:

"a.js", "b.js" and "../c.js" are the JS files to be merged. Merge them into base.js.php. The code in base.js.php is as follows:
Import_static(array(
         'a.js',
         'b.js',
          '../c.js',
'../moduleB/all.js.php' // You can also reference .php files
), __FILE__);
?>

Use in the HTML page to import it.

Before the product goes online, batch files are used for processing, mainly doing two aspects of work
1. Output "*.js.php" to "*.js" file and delete "*.js.php". Command line: php *.js.php > *.js
2. Replace the reference to "*.js.php" in the HTML page with "*.js". preg_replace()

PS: The import_static function solves the problem of include() processing relative paths in PHP.

The above is the entire content of this article, please look forward to subsequent articles for more detailed information

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/911904.htmlTechArticleDetailed explanation of PHP merging static files, configure PHP.ini and change the configuration item (required) auto_prepend_file = "C:xampphtdocsauto_prepend_file.php "Change configuration item (optional) allow_url_include = O...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn