A few ways to read large files in PHP introduction to _php examples

Source: Internet
Author: User

Reading large files has always been a headache for the problem, we like using PHP to read small files can be used directly to implement a variety of functions, but one to large articles will find that the common method is not normal or too long too much card, let's take a look at the PHP read about large file problem solving, I hope the examples will help you.

Scenario: Php reads oversized files, such as 1G log files, where I use 400M access.log files

1. Use File Direct read

<?php
$starttime =microtime_float ();
 
Ini_set (' Memory_limit ', '-1 ');
$file = ' testfile.txt ';
 
$data = file ($file);
$line = $data [Count ($data)-1000];
$endtime =microtime_float ();
 
echo Count ($data), "<br/>";
Echo $endtime-$starttime;
 
function Microtime_float () {
  list ($usec, $sec) = Explode ("", Microtime ());
  Return ((float) $usec + (float) $sec);
>

Operation Result: 10127784 lines were used, 7.8764359951s

My computer is 3G memory, this method is not recommended, because you need to load all the files into memory

2. Use Linux command tail

<?php
 
$starttime =microtime_float ();
 
$file = ' testfile.txt ';
$file = Escapeshellarg ($file); Secure escape of command-line parameters
$line = ' tail-n $file ';
 
echo $line, "<br/>";
 
$endtime =microtime_float ();
Echo $endtime-$starttime;
 
function Microtime_float () {
  list ($usec, $sec) = Explode ("", Microtime ());
  Return ((float) $usec + (float) $sec);
}
 
End

Run results: Only a few milliseconds, easy to handle, this method can not be used under Windows

3. Use fseek function

This is the most common way, it does not need to read the contents of the file into the content, because PHP is written in C, so the implementation is similar to C read the file, through the movement of the pointer, so efficiency is quite efficient. There are a number of different ways to manipulate a file when using fseek, and there may be a slight difference in efficiency,

Here are a few common methods

Method One: Use fopen to open the file (from the file pointer resource handle)

<?php
$starttime =microtime_float ();
 
$file = ' testfile.txt ';
$fp = fopen ($file, "r+");
 
$line = m;
$pos =-2;
$t = $data = "";
 
while ($line > 0)
{
 while ($t!= "\ n")//newline character
 {
 fseek ($fp, $pos, seek_end);/move pointer
 $t = FGETC ($fp //Get a character
 $pos--;//forward offset
 }
 
 $t = "";
 $data = fgets ($FP);//Get the data $line of the current row-
 ;
}
Fclose ($FP);
Echo $data, "<br/>";
$endtime =microtime_float ();
 
Echo $endtime-$starttime;
 
function Microtime_float () {
  list ($usec, $sec) = Explode ("", Microtime ());
  Return ((float) $usec + (float) $sec);
>

Run Result: 0.338493108749

Method Two: A piece of reading

<?php
$starttime =microtime_float ();
 
$file = ' testfile.txt ';
$fp = fopen ($file, "R");
$num = ten;
$chunk = 4096;//4k block
$fs = sprintf ("%u", FileSize ($file));
$readData = ';
$max = (Intval ($fs) = = Php_int_max)? Php_int_max: $fs;
 
for ($len = 0; $len < $max; $len + = $chunk) {
 
 $seekSize = ($max-$len > $chunk)? $chunk: $max-$len;
 Fseek ($fp, ($len + $seekSize) *-1, seek_end);
 $readData = Fread ($fp, $seekSize). $readData;
 
if (Substr_count ($readData, "\ n") >= $num + 1) {
 
  $ns =substr_count ($readData, "\ n")-$num +2;
  Preg_match ('/(. *?\n) {'. $ns. '} /', $readData, $match);
  $data = $match [1];
  break;
}
}
Fclose ($FP);
Echo $data, "<br/>";
 
 
$endtime =microtime_float ();
 
Echo $endtime-$starttime;
 
function Microtime_float () {
  list ($usec, $sec) = Explode ("", Microtime ());
  Return ((float) $usec + (float) $sec);
>

Run Time: 0.00199198722839

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.