如何使用 PHP 获取 CSV 文件中的总行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21447329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the total number of rows in a CSV file with PHP?
提问by telexper
How can I get the total number of rows that are in a CSV file using PHP? I'm using this method but can get it to work properly.
如何使用 PHP 获取 CSV 文件中的总行数?我正在使用这种方法,但可以让它正常工作。
if (($fp = fopen("test.csv", "r")) !== FALSE) {
while (($record = fgetcsv($fp)) !== FALSE) {
$row++;
}
echo $row;
}
回答by Robbie Averill
Here's another option using file()to read the entire file into an array, automatically parsing new lines etc:
这是file()用于将整个文件读入数组,自动解析新行等的另一个选项:
$fp = file('test.csv');
echo count($fp);
Also, since PHP5, you can pass in the FILE_SKIP_EMPTY_LINES... to skip empty lines, if you want to:
此外,从 PHP5 开始,您可以传入FILE_SKIP_EMPTY_LINES... 以跳过空行,如果您想:
$fp = file('test.csv', FILE_SKIP_EMPTY_LINES);
回答by Leo Cavalcante
Create a new file reference using SplFileObject:
使用SplFileObject以下命令创建新文件引用:
$file = new SplFileObject('test.csv', 'r');
Try to seekto the highest Int PHP can handle:
尝试到seekPHP 可以处理的最高 Int:
$file->seek(PHP_INT_MAX);
Then actually it will seek to the highest line it could in the file, there is your last line and the last line + 1 is equals to your total lines:
然后实际上它会寻找文件中的最高行,这是您的最后一行,最后一行 + 1 等于您的总行数:
echo $file->key() + 1;
Tricky, but this will avoid you from loading the file contents into memory, which is a very cool thing to do when dealing with really large files.
棘手,但这将避免您将文件内容加载到内存中,这在处理非常大的文件时是一件非常酷的事情。
回答by Nouphal.M
Try
尝试
$c =0;
$fp = fopen("test.csv","r");
if($fp){
while(!feof($fp)){
$content = fgets($fp);
if($content) $c++;
}
}
fclose($fp);
echo $c;
回答by boesing
I know that this is pretty old, but actually I ran into the same question. As a solution I would assume to use linux specific logic:
我知道这已经很老了,但实际上我遇到了同样的问题。作为解决方案,我假设使用 linux 特定的逻辑:
$rows = shell_exec('$(/bin/which cat) file.csv | $(/bin/which tr) "\r" "\n" | $(which wc) -l');
NOTE: this only works for linux only and this only should be used if you are 100% certain that your file has no multiline-cells
注意:这仅适用于 linux 并且仅当您 100% 确定您的文件没有多行单元格时才应该使用
回答by boesing
CSV rows are separated by line breaks. Therefore, split the rows by line breaks, and you will get an array of rows, which is countable.
CSV 行由换行符分隔。因此,通过换行符拆分行,您将得到一个可数的行数组。
if (($fp = fopen("test.csv", "r")) !== FALSE) {
$rows = explode("\n", $fp);
$length = count($rows);
echo $length;
}
回答by Steve Horvath
Note; none of higher-upvoted solutions that count lines in the file are reliable, as they are only counting the lines, not the csv entries (which can contain newline characters)
笔记; 计算文件中行数的高票解决方案都不是可靠的,因为它们只计算行数,而不是 csv 条目(可以包含换行符)
I'm using a similar solution to op, and it works perfectly, but with op's code the while part can break on empty lines, which is potentially his problem.
我正在使用与 op 类似的解决方案,它运行良好,但是使用 op 的代码,while 部分可能会在空行上中断,这可能是他的问题。
So it looks like this (edited op's code)
所以它看起来像这样(编辑操作的代码)
$rowCount=0;
if (($fp = fopen("test.csv", "r")) !== FALSE) {
while(!feof($fp)) {
$data = fgetcsv($fp , 0 , ',' , '"', '"' );
if(empty($data)) continue; //empty row
$rowCount++;
}
fclose($fp);
}
echo $rowCount;
回答by Rosamunda
I know this is an old post, but I've been googling this issue, and found that the only problem with the original code was that you need to define $rowoutside the while loop, like this:
我知道这是一篇旧帖子,但我一直在谷歌搜索这个问题,发现原始代码的唯一问题是你需要$row在 while 循环之外定义,如下所示:
if (($fp = fopen("test.csv", "r")) !== FALSE) {
$row = 1;
while (($record = fgetcsv($fp)) !== FALSE) {
$row++;
}
Just in case it helps someone :) echo $row; }
以防万一它对某人有帮助:) echo $row; }
回答by kimoduor
In case you are getting the file from a form
如果您从表单中获取文件
$file = $_FILES['csv']['tmp_name'];
$fp = new SplFileObject($file, 'r');
$fp->seek(PHP_INT_MAX);
echo $fp->key() + 1;
$fp->rewind();
Works like charm!!!!!!!!!!!!!!!!!!
像魅力一样工作!!!!!!!!!!!!!!!!!!
回答by Gagandeep cheema
$filename=$_FILES['sel_file']['tmp_name'];
$file=fopen($filename,"r");
$RowCount=0;
while ((fgetcsv($file)) !== FALSE)
{
$RowCount++;
}
echo $RowCount;
fclose($file);
回答by Sagar Patil
count(file('filename.csv', FILE_SKIP_EMPTY_LINES));
计数(文件('文件名.csv',FILE_SKIP_EMPTY_LINES));

