PHP merge static file detailed _php example

Source: Internet
Author: User
Tags html page

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.