Home > Backend Development > PHP Tutorial > PHP merge static files_PHP tutorial

PHP merge static files_PHP tutorial

WBOY
Release: 2016-07-13 17:50:23
Original
896 people have browsed it

Configure PHP.ini
Change the 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

1. 2. /**
3. *Introduce static files
4. * @param {array|string} relative path
5. * @param {string} The path where the currently executed script is located __FILE__
6. *
7.*/
8. function import_static($files, $path=NULL){
9. // Change the execution path of the current script
10. $old_dir = getcwd();
11. $tmp_dir = (isset($path)) ? dirname($path): dirname(__FILE__);
12. chdir($tmp_dir);
13. // Organize included files
14. if (!is_array($files)) {
15.         $tmp = array();
16.           $tmp[] = $files;                              17. $files = $tmp;
18. }  
19. //Send header information
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. //Introduce the included file www.2cto.com
31. foreach($files as $key=>$value) {
32. require_once($value);
33. }  
34. //Change back the execution path of the current script
35. chdir($old_dir);
36. }
37. ?>

How to use
"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:

1. 2. import_static(array(

3. 'a.js',

4. 'b.js',
5. '../c.js',
6. '../moduleB/all.js.php' // You can also reference .php files
7. ), __FILE__);
8. ?>
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.
to be continued. . .

Excerpted from Rain Man



http://www.bkjia.com/PHPjc/478272.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478272.htmlTechArticleConfigure PHP.ini Change configuration items (required) auto_prepend_file = C:xampphtdocsauto_prepend_file.php Change configuration items (optional) allow_url_include = On auto_prepend_file.php file content...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template