Php: The best way to read large files _ PHP Tutorial

Source: Internet
Author: User
Php provides the best way to read large files. Php is the best way to read large files. php is the best way to read large files. we generally write all the files into the memory in one row instead of writing them into the memory. this will cause the php program to kill, the best way for php to read large files

Php reads large files in a row instead of writing all the files into the memory at a time. This will cause the php program to become stuck. The following is an example.

The code is as follows:

Read the last few rows of data from a large file:
/**
* Get the last $ n lines of the file
* @ Param string $ filename File path
* @ Param int $ n the last few rows
* @ Return mixed false indicates an error occurred. if the request succeeds, a string is returned.
*/
Function FileLastLines ($ filename, $ n ){
If (! $ Fp = fopen ($ filename, 'r ')){
Echo "failed to open the file. Please check whether the file path is correct and the path and file name do not contain Chinese characters ";
Return false;
}
$ Pos =-2;
$ Eof = "";
$ Str = "";
While ($ n> 0 ){
While ($ eof! = "N "){
If (! Fseek ($ fp, $ pos, SEEK_END )){
$ Eof = fgetc ($ fp );
$ Pos -;
} Else {
Break;
}
}
$ Str. = fgets ($ fp );
$ Eof = "";
$ N -;
}
Return $ str;
}

Echo nl2br(FileLastLines('sss.txt ', 4 ));
/*** Get the last $ n line of the file * @ param string $ filename File path * @ param int $ n the last few lines * @ return mixed false indicates an error occurred, returns the string */function FileLastLines ($ filename, $ n) {if (! $ Fp = fopen ($ filename, 'r') {echo "failed to open the file. Please check whether the file path is correct and the path and file name do not contain Chinese characters"; return false ;} $ pos =-2; $ eof = ""; $ str = ""; while ($ n> 0) {while ($ eof! = "N") {if (! Fseek ($ fp, $ pos, SEEK_END) {$ eof = fgetc ($ fp); $ pos --;} else {break;} $ str. = fgets ($ fp); $ eof = ""; $ n --;} return $ str;} echo nl2br(FileLastLines('sss.txt ', 4 ));

Function tail ($ fp, $ n, $ base = 5)
{
Assert ($ n> 0 );
$ Pos = $ n + 1;
$ Lines = array ();
While (count ($ lines) <= $ n ){
Try {
Fseek ($ fp,-$ pos, SEEK_END );
} Catch (Exception $ e ){
Fseek (0 );
Break;
}
$ Pos * = $ base;
While (! Feof ($ fp )){
Array_unshift ($ lines, fgets ($ fp ));
}
}
Return array_slice ($ lines, 0, $ n );
}
Var_dump (tail (fopen ("access. log", "r +"), 10 ));
$ Fp = fopen ($ file, "r ");
$ Line = 10;
$ Pos =-2;
$ T = "";
$ Data = "";
While ($ line> 0 ){
While ($ t! = "N "){
Fseek ($ fp, $ pos, SEEK_END );
$ T = fgetc ($ fp );
$ Pos --;
}
$ T = "";
$ Data. = fgets ($ fp );
$ Line --;
}
Fclose ($ fp );
Echo $ data

Using php to read large files, we generally extract all the files into the memory in one row instead of writing them into the memory. this will cause the php program to become stuck ,...

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.