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.