Saviola wrote:Advantage of fread() is that you can read just a said amount of bytes from a line - you don't have to read the full file at once.
You can, yes, but that's not what you're doing. In the code you posted above, you're fread()'ing the entire file. And besides, for a CSV, why would you want to read a fixed number of bytes rather than entire lines at a time?
Since you're reading the entire file in your code above, I referenced [man]file_get_contents/man for this reason:
PHP Manual wrote:file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Saviola wrote:About fgetcsv(), i don't like at all to try to define the "optional" max length of the line..
Again, you'd be doing the same thing with fread() anyway, so why not?
Saviola wrote:This causes a problem on record 2
The problem isn't with fgetcsv(), because normal CSV formats denote a double quote as a way to explicitly enclose a given column's data where it might otherwise be ambiguous (e.g. if the data itself contained the delimiter character, you'd want to surround it with quotes).
So if you're expecting to read data like that, then yeah, you can't use fgetcsv() because it's not a proper CSV format.
Saviola wrote:Another Note that fgetcsv, at least in PHP 5.3 or previous, will NOT work with UTF-16 encoded files.
Considering that PHP 5 as a whole isn't unformly Unicode-friendly (that's what PHP 6 is for, supposedly), I don't really fault fgetcsv() for that shortcoming.
Saviola wrote:You can esay change this ... just have to change $fieldseparator
That's not what I meant. If you have properly formed CSV data, like so:
name,point,comment
John Smith,"38.222,43.666","Hi, friends!"
your code would give me 5 columns, whereas fgetcsv() would correctly give me 3.