[Practical code] PHP reads a 1 GB file size,

Source: Internet
Author: User

[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

 

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.