[Get the number of rows]php read large files to provide a performance method, PHP's Stream_get_line function reads large files to get the number of lines of the file ...

Source: Internet
Author: User

Background:
Here's how to get the number of rows in a file:
If a file knows a few lines, it can control the data that gets a certain number of rows and then put it into the database. This way, regardless of the performance of reading large files, or the performance of writing to the database, can be greatly improved.
Here's how to get the number of rows in a file

' Error.log '; $fp = fopen ($temp _file, die("Open File failure!"); 0; if ($fp) {/ * Gets a line of the file, note: PHP5 is required to support the function; */While (Stream_get_line ("\ r \ n")) { $tota    l_line++; } fclose ($fp);}           

So, what's the next step?
The following programs are mainly 1000 each time the maximum, the remainder of less than 1000 in the remainder.
Storage 10W Data time is only a few seconds, so that performance is greatly drops of good.
Define' Each_num ',1000);/* Number of bars per storage */if (!$total _line)Die' No record! ');$logs = mod (' Logs_error ');$temp = Array ();$num = Ceil ($total _line/each_num);$mod = Fmod ($total _line,each_num);For$i =0;$i <$num;$i + +) {if (($i +1) = =$num &&$mod) {$temp =$logs->getfilelines ($temp _file,$i*each_num+1,$MOD);$insert _num + = $mod;     } else{         $temp =  $logs->getfilelines ( $temp _file,  $i *each_num+1, ( $i +1) *each_num);         $ Insert_num + = each_num;    }    /* warehousing */      $logs->insert ( $temp);}  @unlink ( $temp _file); Echo  "Insert record:{< span class= "variable" > $insert _num} \nsuccess "; exit ();               

If you have a better way to manipulate the file, or is better in the way of warehousing, you can contact me, we exchange.
From: Http://www.tuicool.com/articles/yQn2q2 method Two: Making system calls
$file = ' xxx.file ';
List ($size, $file) = Explode ("", System ("Wc-l $file"));
$size is the number of rows in the file

[Get the number of rows]php read large files to provide a performance method, PHP's Stream_get_line function reads large files to get the number of lines of the file ...

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.