Php to read large files

Source: Internet
Author: User
This article mainly introduces how php can read large files. For more information, see

This article mainly introduces how php can read large files. For more information, see

Generally, when php reads large files, the method we use is generally to fetch a row, rather than writing all the files into the memory at a time. This will cause the php program to kill, the following is an example.

Read the last few rows of data from a large file:

<? Php/*** 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 )); /***** get the last $ n lines of the file *** @ param string $ filename file path *** @ param int $ n the last few lines ** @ return mixed false indicates there are error, 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;?>

Readers can improve and improve the instance based on its characteristics to better meet their application requirements.

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.