如何在PHP中读取大文件之前获取总行数

时间:2022-03-30 09:19:44

I am already using this example of how to read large data files in PHP line by line

我已经在使用这个如何逐行读取PHP中的大数据文件的示例

Now, what it'd like to do, is obtain the total number of rows in the file so that I may display a percentage complete or at least what the total number of rows are so I can provide some idea of how much processing is left to be done.

现在,它想要做的是获取文件中的总行数,以便我可以显示完整的百分比或至少是总行数,以便我可以提供一些关于剩余多少处理的想法要完成。

Is there a way to get the total number of rows without reading in the entire file twice? (once to count the rows and once to do the processing)

有没有办法在没有读取整个文件两次的情况下获得总行数? (一次计算行数,一次进行处理)

5 个解决方案

#1


Poor mans answer:

穷人回答:

No, but you can estimate. Calc a simple average reading (use the first 250 lines) and go with that.

不,但你可以估计。计算一个简单的平均读数(使用前250行)并使用它。

estNumOfLines = sizeOfFile / avgLineSize

You could store off the number of lines in the file when you are creating the file...

您可以在创建文件时存储文件中的行数...

Alternatively, you could display the number of KB processed, and that would be perfectly accurate.

或者,您可以显示已处理的KB数,这将是完全准确的。

#2


You can determine the size of the file, then guage your progress through it by adding up the size of your reads:

您可以确定文件的大小,然后通过累加读取的大小来衡量您的进度:

$fname = 'foofile.txt';
$fsize = filesize($fname);
$count = 0;
$handle = fopen($fname, "r") or die("Couldn't get handle");
if ($handle) {
  while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    // Process buffer here..
    $count++;
    echo ($count * 4096)/$fsize . " percent read.";
  }
  fclose($handle);
}

Note: code adapted from referenced answer

注意:代码改编自参考答案

#3


Is there any reason you need to count rows and not bytes? If all you want to know is "percent done", just track it the by number bytes read/total bytes.

你有什么理由需要计算行而不是字节?如果您只想知道“完成百分比”,则只需按字节读取数/字节数来跟踪它。

#4


use the linux command wc -l filename.txt This will output the number of lines in a file.

使用linux命令wc -l filename.txt这将输出文件中的行数。

#5


How would you know the number of pages in a book, without counting them?
You would measure the width of a page and the width of the book and divide one by the other.

你怎么知道书中的页数,而不计算它们?您可以测量页面的宽度和书籍的宽度,然后将它们分开。

Same here, calculate the average line length from the first few lines, then do the same math with the file size...

同样在这里,计算前几行的平均线长,然后用文件大小做同样的数学运算......

#1


Poor mans answer:

穷人回答:

No, but you can estimate. Calc a simple average reading (use the first 250 lines) and go with that.

不,但你可以估计。计算一个简单的平均读数(使用前250行)并使用它。

estNumOfLines = sizeOfFile / avgLineSize

You could store off the number of lines in the file when you are creating the file...

您可以在创建文件时存储文件中的行数...

Alternatively, you could display the number of KB processed, and that would be perfectly accurate.

或者,您可以显示已处理的KB数,这将是完全准确的。

#2


You can determine the size of the file, then guage your progress through it by adding up the size of your reads:

您可以确定文件的大小,然后通过累加读取的大小来衡量您的进度:

$fname = 'foofile.txt';
$fsize = filesize($fname);
$count = 0;
$handle = fopen($fname, "r") or die("Couldn't get handle");
if ($handle) {
  while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    // Process buffer here..
    $count++;
    echo ($count * 4096)/$fsize . " percent read.";
  }
  fclose($handle);
}

Note: code adapted from referenced answer

注意:代码改编自参考答案

#3


Is there any reason you need to count rows and not bytes? If all you want to know is "percent done", just track it the by number bytes read/total bytes.

你有什么理由需要计算行而不是字节?如果您只想知道“完成百分比”,则只需按字节读取数/字节数来跟踪它。

#4


use the linux command wc -l filename.txt This will output the number of lines in a file.

使用linux命令wc -l filename.txt这将输出文件中的行数。

#5


How would you know the number of pages in a book, without counting them?
You would measure the width of a page and the width of the book and divide one by the other.

你怎么知道书中的页数,而不计算它们?您可以测量页面的宽度和书籍的宽度,然后将它们分开。

Same here, calculate the average line length from the first few lines, then do the same math with the file size...

同样在这里,计算前几行的平均线长,然后用文件大小做同样的数学运算......