Reading files is important in PHP, and reading larger memory files requires some skill, which is explained in this article.
Because the file function is a one-time read all the content into memory, and PHP in order to prevent some of the poor written program to take up too much memory and cause the system memory shortage, so that the server is down, so think of a good way.
01 idea 1: Use PHP to execute Linux commands, copy one file content (A.log) to another file (B.log) Cat A.log >>b.log02 idea 2: Execute the Linux head command with PHP, get the content, A row of rows is written to another file (B.log). Cat a.log|wc-lsed-n ' 1 P ' a.logfileputcontents (' B.log ', $content, file_append);
02. Read 1G of File contents public function Testgetlarge () {/*{{{*/#01. Method 1 Direct Cat post redirect #if (' usecat ' = = ' Usecat ') if (0 & amp;& ' usecat ' = = ' Usecat ') {' cat a.log>>b.log '; } #02. With Flie_put_contents, a line reads and appends the write if (' usefileputcontents ' = = ' usefileputcontents ') {/*{{{*/$lineNum = ' cat a.log|wc-l '; for ($i =1; $i <= $lineNum; $i + +) { $content = ' sed-n ' $i p ' a.log '; File append file_put_contents (' B.log ', $content, file_append); } } /*}}}*/
This article detailed the PHP implementation to read a 1G file size method, more relevant knowledge, please follow the PHP Chinese web.