PHP read a large file class Splfileobject use introduction _php instance

Source: Internet
Author: User
Tags spl

If the loaded file is particularly large, such as hundreds of M, on G, then performance down, then PHP has no large file processing functions or classes? The answer is: yes.
PHP is really more and more "object-oriented", some of the original basis of the SPL methods are beginning to realize class.
Starting with the PHP 5.1.0, the SPL library adds Splfileobject and splfileinfo two standard file operation classes. Splfileinfo is implemented from the PHP 5.1.2.
In the literal sense, it can be seen that splfileobject is more powerful than splfileinfo.
Yes, Splfileinfo is used only to get some property information about a file, such as file size, file access time, file modification time, suffix name equivalence, and Splfileobject is inherited splfileinfo these features.

Copy Code code as follows:
/** returns the contents of the file from line x to row y (support php5, PHP4)
* @param string $filename filename
* Number of lines @param int $startLine start
* Number of lines @param int $endLine end
* @return String
*/
function Getfilelines ($filename, $startLine = 1, $endLine =50, $method = ' rb ') {
$content = Array ();
$count = $endLine-$startLine;
Determine the PHP version (because you want to use splfileobject,php>=5.1.0)
if (Version_compare (php_version, ' 5.1.0 ', ' >= ')) {
$fp = new Splfileobject ($filename, $method);
$FP->seek ($startLine-1);//go to Nth row, the Seek method parameter counts starting from 0
for ($i = 0; $i <= $count; + + $i) {
$content []= $fp->current ()//current () get the contents of the line
$FP->next ()//Next line
}
}else{//php<5.1
$fp = fopen ($filename, $method);
if (! $fp) return to ' Error:can not read file ';
For ($i =1 $i < $startLine + + $i) {//Skip front $startline Line
Fgets ($FP);
}
for ($i; $i <= $endLine; + + $i) {
$content []=fgets ($FP);//Read file line contents
}
Fclose ($FP);
}
Return Array_filter ($content); Array_filter filter: False,null, "
}

Ps: None of the above "read to the end of the judgment":! $fp->eof () or!feof ($FP), coupled with this judgment impact efficiency, you add a lot of tests and many many lines of running time to know, and there is absolutely no need to add.
You can see from the above function that the use of splfileobject is much faster than the fgets below, especially if the number of rows is very high, and to take the later content. Fgets to two cycles to be able to, and to cycle $endline times.
This method took a lot of effort, testing a lot of writing, is to get the most efficient method. Who feel that there is merit to improve the welcome enlighten.
Use to return 35270 rows-35280 lines of content:
Copy Code code as follows:
Echo ' <pre> ';
Var_dump (Getfilelines (' test.php ', 35270,35280));
Echo ' </pre> ';

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.