How do i count the number of rows in a csv file in php?

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;
}

How do i count the number of rows in a csv file in php?

Toto

87.1k61 gold badges86 silver badges120 bronze badges

asked Jan 30, 2014 at 3:35

How do i count the number of rows in a csv file in php?

1

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.

answered Mar 28, 2017 at 17:18

Leo CavalcanteLeo Cavalcante

2,1572 gold badges24 silver badges30 bronze badges

8

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

answered Jan 30, 2014 at 3:40

How do i count the number of rows in a csv file in php?

scrowlerscrowler

24.1k9 gold badges62 silver badges91 bronze badges

4

Try

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

answered Jan 30, 2014 at 3:48

How do i count the number of rows in a csv file in php?

Nouphal.MNouphal.M

6,2981 gold badge15 silver badges27 bronze badges

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

answered Mar 25, 2015 at 14:43

boesingboesing

3153 silver badges10 bronze badges

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;
}

answered Jan 30, 2014 at 3:37

1

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;

answered Dec 4, 2019 at 3:42

2

I find this the most reliable:

$file = new SplFileObject('file.csv', 'r');
$file->setFlags(
    SplFileObject::READ_CSV |
    SplFileObject::READ_AHEAD |
    SplFileObject::SKIP_EMPTY |
    SplFileObject::DROP_NEW_LINE
);

$file->seek(PHP_INT_MAX);

$lineCount = $file->key() + 1;

answered Dec 17, 2020 at 1:16

JonathanJonathan

12.6k16 gold badges85 silver badges119 bronze badges

2

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; }

answered Mar 16, 2017 at 3:04

How do i count the number of rows in a csv file in php?

RosamundaRosamunda

14.2k10 gold badges36 silver badges66 bronze badges

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

answered Jun 7, 2017 at 14:40

How do i count the number of rows in a csv file in php?

kimoduorkimoduor

4155 silver badges12 bronze badges

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

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

echo $RowCount;
fclose($file);

How do i count the number of rows in a csv file in php?

Komal12

3,2574 gold badges14 silver badges25 bronze badges

answered Jul 4, 2017 at 9:56

How do i count the number of rows in a csv file in php?

1

How do I count records in a CSV file in PHP?

Method #1: Parsing the entire CSV file at once using PHP This first method uses the file() function to read the contents of the file into an array, from which the count() method can be used to derive the line count: $linecount = count(file('filename. csv')); Reading an entire file into memory just to get a line count?

How do I count the number of rows in a CSV file?

Using len() function Under this method, we need to read the CSV file using pandas library and then use the len() function with the imported CSV file, which will return an int value of a number of lines/rows present in the CSV file.

What is Fgetcsv function in PHP?

The fgetcsv() function can parse a line from an open file and check for CSV fields. This function stops returning on a new line at a specified length or EOF, whichever comes first. This function returns CSV fields in the array on success or false on failure and EOF.

How do I count the number of columns in a CSV file?

To get the number of rows, and columns we can use len(df.

Tải thêm tài liệu liên quan đến bài viết How do i count the number of rows in a csv file in php?