How can I get the total number of rows in a CSV file with PHP?

可紊 提交于 2019-12-17 19:06:42

问题


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.

if (($fp = fopen("test.csv", "r")) !== FALSE) { 
  while (($record = fgetcsv($fp)) !== FALSE) {
      $row++;
  }

  echo $row;
}

回答1:


Here's another option using file() to read the entire file into an array, automatically parsing new lines etc:

$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:

$fp = file('test.csv', FILE_SKIP_EMPTY_LINES);

Manual: http://php.net/manual/en/function.file.php




回答2:


Create a new file reference using SplFileObject:

$file = new SplFileObject('test.csv', 'r');

Try to seek to the highest Int PHP can handle:

$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:

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.




回答3:


Try

$c =0;
$fp = fopen("test.csv","r");
if($fp){
    while(!feof($fp)){
          $content = fgets($fp);
      if($content)    $c++;
    }
}
fclose($fp);
echo $c;



回答4:


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:

$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




回答5:


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.

if (($fp = fopen("test.csv", "r")) !== FALSE) { 
    $rows = explode("\n", $fp);
    $length = count($rows);

    echo $length;
}



回答6:


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 $row outside the while loop, like this:

if (($fp = fopen("test.csv", "r")) !== FALSE) { 
$row = 1;
  while (($record = fgetcsv($fp)) !== FALSE) {
      $row++;
  }

Just in case it helps someone :) echo $row; }




回答7:


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)

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.

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;



回答8:


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!!!!!!!!!!!!!!!!!!




回答9:


$filename=$_FILES['sel_file']['tmp_name'];
$file=fopen($filename,"r");
$RowCount=0;

while ((fgetcsv($file)) !== FALSE) 
{
    $RowCount++;
}

echo $RowCount;
fclose($file);



回答10:


count(file('filename.csv', FILE_SKIP_EMPTY_LINES));



来源:https://stackoverflow.com/questions/21447329/how-can-i-get-the-total-number-of-rows-in-a-csv-file-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!