Usage of SplFileObject for PHP to read large files

Source: Internet
Author: User
Usage of SplFileObject for PHP to read large files

  1. /** Return the content of the file from row X to Row Y (php5 and php4 are supported)
  2. * @ Param string $ filename file name
  3. * @ Param int $ number of rows starting with startLine
  4. * @ Param int $ number of rows ending with endLine
  5. * @ Return string
  6. */
  7. Function getFileLines ($ filename, $ startLine = 1, $ endLine = 50, $ method = 'RB '){
  8. $ Content = array ();
  9. $ Count = $ endLine-$ startLine;
  10. // Determine the php version (because SplFileObject is used, PHP> = 5.1.0)
  11. If (version_compare (PHP_VERSION, '5. 1.0 ','> = ')){
  12. $ Fp = new SplFileObject ($ filename, $ method );
  13. $ Fp-> seek ($ startLine-1); // go to row N, the seek method parameter starts counting from 0
  14. For ($ I = 0; $ I <= $ count; ++ $ I ){
  15. $ Content [] = $ fp-> current (); // current () get the content of the current row
  16. $ Fp-> next (); // next row
  17. }
  18. } Else {// PHP <5.1
  19. $ Fp = fopen ($ filename, $ method );
  20. If (! $ Fp) return 'Error: can not read file ';
  21. For ($ I = 1; $ I <$ startLine; ++ $ I) {// skip the previous $ startLine line
  22. Fgets ($ fp );
  23. }
  24. For ($ I; $ I <= $ endLine; ++ $ I ){
  25. $ Content [] = fgets ($ fp); // read the content of the file row
  26. }
  27. Fclose ($ fp );
  28. }
  29. Return array_filter ($ content); // array_filter: false, null ,''
  30. }

Note: The above does not add "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 code is returned:

  1. Echo'
    ';
  2. var_dump(getFileLines('test.php',35270,35280));
  3. echo '
  4. ';

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.