How does php write the content of multiple php files to one file?

Source: Internet
Author: User
Tags php write
How does php write the content of multiple php files to one file? these cache php files are all stored in the cache Directory, for example:
A. php
B. php
C. php

The content in these php files is some cached data, which is written as an array during my storage:
Example: a. php file
$ Locale R = array (
'AAA' => '123 ',
'BBB '=> '123 ',
'CCC '=> '123 ',
);
?>

Example: B. php file
$ BArr = array (
'AAA' => '123 ',
'BBB '=> '123 ',
'CCC '=> '123 ',
);
?>

Example: c. php file
$ CArr = array (
'AAA' => '123 ',
'BBB '=> '123 ',
'CCC '=> '123 ',
);
?>

Now I want to write all the php files in this directory to a php file.
Below is the code I wrote, but after writing the file, I found that the php mark of the file is also written into it. I also escaped the array format of the cached file.
How can the master modify it,



// Create a cache directory $ dir = "cache"; $ fileDir = @ opendir ($ dir); if (! $ FileDir) {echo 'failed to open the directory '; exit;} while (false! ==( $ File = @ readdir ($ fileDir) {// skip the current directory and the upper-level directory if ('. '==$ file | '.. '===$ file) continue; // get the file suffix $ filetype = substr ($ file, strripos ($ file ,". ") + 1); // file path $ filepath = $ dir. '/'. $ file; // Determine whether the php file exists and whether the if ($ filetype = 'php' & is_file ($ filepath) {$ handle = fopen ($ filepath, "r"); $ contents. = fread ($ handle, filesize ($ filepath); fclose ($ handle) ;}} write ('data. php ',"
 ");


Reply to discussion (solution)

You can filter PHP IDs like this.
Change row 18th:

$contents.= rtrim(ltrim(fread($handle,filesize($v)),'
 ');


The escape is because of the var_export function.

Filter out It is unnecessary, but sometimes it will go wrong (if there is data)

$dir = "cache";$out = 'data.php';file_put_contents($out);foreach(glob("$dir/*.php") as $fn) {  file_put_contents($out, file_get_contents($fn), FILE_APPEND);}

Filter out It is unnecessary, but sometimes it will go wrong (if there is data)

$dir = "cache";$out = 'data.php';file_put_contents($out);foreach(glob("$dir/*.php") as $fn) {  file_put_contents($out, file_get_contents($fn), FILE_APPEND);}




Writing code in this way seems to be more efficient than writing code in my way. I learned it !!
But if you do not delete the php identifier, the generated file will be like this? How can we improve it ??

  '111',  'bbb' => '222',  'ccc' => '333',);?>
  '111',  'bbb' => '222',  'ccc' => '333',);?>
  '111',  'bbb' => '222',  'ccc' => '333',);?>
  '111',  'bbb' => '222',  'ccc' => '333',);?>


// Obtain the file suffix and determine whether it is a php suffix if (substr ($ file,-4) = '. php '){.........}


I think it's okay to define a class and capture data, but it's not complicated.

Str_replace first replaces the PHP identifier with null, and then it is simple.

Related 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.