Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensure fgetcsv() reads the entire line

Tags:

php

csv

I am using PHP to import data from a CSV file using fgetcsv(), which yields an array for each row. Initially, I had the character limit set at 1024, like so:

while ($data = fgetcsv($fp, 1024)) {
  // do stuff with the row
}

However, a CSV with 200+ columns surpassed the 1024 limit on many rows. This caused the line read to stop in the middle of a row, and then the next call to fgetcsv() would start where the previous one left off and so on until an EOL was reached.

I have since upped this limit to 4096, which should take care of the majority of cases, but I would like put a check in to be sure that the entire line was read after each line is fetched. How do I go about this?

I was thinking to check the end of the last element of the array for end of line characters (\n, \r, \r\n), but wouldn't these be parsed out by the fgetcsv() call?

like image 912
Danja Garno Avatar asked May 24 '12 22:05

Danja Garno


People also ask

What does Fgetcsv do in PHP?

The fgetcsv() function parses a line from an open file, checking for CSV fields.

How do I read a csv file in column wise in PHP?

You can open the file using fopen() as usual, get each line by using fgets() and then simply explode it on each comma like this: <? php $handle = @fopen("/tmp/inputfile. txt", "r"); if ($handle) { while (($buffer = fgets($handle)) !==

How do you check csv file is empty or not in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.


2 Answers

Just omit the length parameter. It's optional in PHP5.

while ($data = fgetcsv($fp)) {
  // do stuff with the row
}
like image 81
Rocket Hazmat Avatar answered Oct 10 '22 14:10

Rocket Hazmat


Just don't specify a limit, and fgetcsv() will slurp in as much as is necessary to capture a full line. If you do specify a limit, then it's entirely up to YOU to scan the file stream and ensure you're not slicing something down the middle.

However, note that not specifying a limit can be risky if you don't have control over generation of this .csv in the first place. It'd be easy to swamp your server with a malicious CSV that has a many terabytes of data on a single line.

like image 34
Marc B Avatar answered Oct 10 '22 15:10

Marc B