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.
87.1k61 gold badges86 silver badges120 bronze badges
asked Jan 30, 2014 at 3:35
Create a new file reference using
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:
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
Here's another option using
Also, since PHP5, you can pass in the
answered Jan 30, 2014 at 3:40
24.1k9 gold badges62 silver badges91 bronze badges
answered Jan 30, 2014 at 3:48
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:
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
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.
answered Jan 30, 2014 at 3:37
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)
answered Dec 4, 2019 at 3:42
I find this the most reliable:
answered Dec 17, 2020 at 1:16
12.6k16 gold badges85 silver badges119 bronze badges
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
Just in case it helps someone :) echo $row; }
answered Mar 16, 2017 at 3:04
14.2k10 gold badges36 silver badges66 bronze badges
In case you are getting the file from a form
Works like charm!!!!!!!!!!!!!!!!!!
answered Jun 7, 2017 at 14:40
4155 silver badges12 bronze badges
3,2574 gold badges14 silver badges25 bronze badges
answered Jul 4, 2017 at 9:56
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.