Read a 1G file size with PHP implementation

Source: Internet
Author: User
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.

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.