[Practical code] PHP reads a 1 GB file size,
Address: http://www.cnblogs.com/aiweixiao/p/7535351.html
Please pay attention to my public account "programmer's cultural sentiments"Http://t.cn/RotyZtu
[Background]: The file function reads all the content into the memory at a time. To prevent some poorly written programs from occupying too much memory, php causes insufficient system memory, so that the server is down, you need to find a good way.
[Idea ]:
01 Idea 1: Use php to execute linux commands. log) to another file (B. log) cat. log> B. log02 idea 2: Use php to execute the linux head command, get the content, and write one row into another file (B. log. Cat a. log | wc-lsed-n '1 P' a. logfileputcontents ('B. log', $ content, FILE_APPEND );
[Code implementation ]:
1 // 02. read 1g file content 2 public function testGetLarge () 3 {/* {*/4 #01. method 1 direct cat and redirect 5 # if ('usecat' = 'usecat') 6 if (0 & 'usecat' = 'usecat ') 7 {8 'cat. log> B. log'; 9} 10 11 #02. use flie_put_contents to read and append data to 12 if ('usefileputcontents' = 'usefileputcontents') 13 {/* {*/14 $ lineNum = 'cat. log | wc-l '; 15 for ($ I = 1; $ I <= $ lineNum; $ I ++) 16 {17 $ content = 'sed-n' $ I P'. log '; 18 // append 19 file_put_contents (' B. log', $ content, FILE_APPEND); 20} 21 22 }/*}}}*/
[Other solutions]: http://www.jb51.net/article/40847.htm