PHP reads large file program code

Source: Internet
Author: User
Tags fread spl

Generally, we use fopen or file_get_contents to read files. The former can be read cyclically, and the latter can be read at one time, but all files are loaded at one time. If the size of the file to be loaded is very large, such as several hundred MB or GB, the performance will be reduced. Is there any processing function or class for large files in PHP? The answer is: yes.

PHP is becoming more and more "object-oriented", and some of the original basic SPL methods are gradually implementing class.

Starting from PHP 5.1.0, the SPL Library has added two standard file operation classes: SplFileObject and SplFileInfo. SplFileInfo is implemented from PHP 5.1.2.

From the literal meaning, we can see that SplFileObject is more powerful than SplFileInfo.

Yes. SplFileInfo is only used to obtain some attributes of a file, such as the file size, file access time, file modification time, and suffix name. SplFileObject inherits the SplFileInfo functions.

The code is as follows: Copy code

/** Return the content of the file from row X to Row Y (php5 and php4 are supported)
* @ Param string $ filename file name
* @ Param int $ number of rows starting with startLine
* @ Param int $ number of rows ending with endLine
* @ Return string
*/
Function getFileLines ($ filename, $ startLine = 1, $ endLine = 50, $ method = 'RB '){
$ Content = array ();
$ Count = $ endLine-$ startLine;
// Determine the php version (because SplFileObject is used, PHP> = 5.1.0)
If (version_compare (PHP_VERSION, '5. 1.0 ','> = ')){
$ Fp = new SplFileObject ($ filename, $ method );
$ Fp-> seek ($ startLine-1); // go to row N, the seek method parameter starts counting from 0
For ($ I = 0; $ I <= $ count; ++ $ I ){
$ Content [] = $ fp-> current (); // current () get the content of the current row
$ Fp-> next (); // next row
        }
} Else {// PHP <5.1
$ Fp = fopen ($ filename, $ method );
If (! $ Fp) return 'Error: can not read file ';
For ($ I = 1; $ I <$ startLine; ++ $ I) {// skip the previous $ startLine line
Fgets ($ fp );
        }
For ($ I; $ I <= $ endLine; ++ $ I ){
$ Content [] = fgets ($ fp); // read the content of the file row
        }
Fclose ($ fp );
    }
Return array_filter ($ content); // array_filter: false, null ,''
}  

Ps: none of the above are added. "read to the end of the judgment ":! $ Fp-> eof () or! Feof ($ fp), coupled with this judgment, affects the efficiency. You can test the running time of many and many rows by yourself, and it is completely unnecessary to add it here.

From the above function, we can see that the use of SplFileObject is much faster than the fgets below, especially when the number of file lines is large and the subsequent content is to be retrieved. Fgets requires two loops and $ endLine loops.

This method took a lot of effort to test a lot of writing, that is, to find the most efficient method. Anyone who thinks it is worthy of improvement should give us some advice.

If yes, the following content is returned:

The code is as follows: Copy code

Echo '<pre> ';
Var_dump (getFileLines ('test. Php', 35270,35280 ));
Echo '</pre> ';

Let's look at another instance.

The code is as follows: Copy code

 

Function readBigFile ($ filename, $ count = 20, $ tag = "rn "){
$ Content = ""; // final content
$ Current = ""; // store the currently read content
$ Step = 1; // The number of characters each time
$ TagLen = strlen ($ tag );
$ Start = 0; // start position
$ I = 0; // counter
$ Handle = fopen ($ filename, 'R + '); // open the file in read/write mode. The pointer points to the start position of the file.
While ($ I <$ count &&! Feof ($ handle )){
Fseek ($ handle, $ start, SEEK_SET); // The pointer is set at the beginning of the file.
$ Current = fread ($ handle, $ step); // read the file
$ Content. = $ current; // composite string
$ Start + = $ step; // move forward by step
// Extract the last few characters of a string based on the delimiter length
$ SubstrTag = substr ($ content,-$ tagLen );
If ($ substrTag ==$ tag) {// determines whether it is a line break or another separator
$ I ++;
$ Content. = "<br/> ";
}
}
// Close the file
Fclose ($ handle );
// Return results
Return $ content;
}
$ Filename = "csdn. SQL"; // file to be read
$ Tag = "n"; // line separator note that double quotation marks must be used here
$ Count = 100; // Number of read rows
$ Data = readBigFile ($ filename, $ count, $ tag );
Echo $ data;

Note: by combining fseek and fread of PHP, you can read a part of data in the file at will. The value of the variable $ tag passed in by the function is different from that of the system, the input values are also different: "rn" for Windows, "n" for linux/unix, and "r" for Mac OS ".

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.