CSV Import issue


I am using fgetcsv function to import csv files in my application, and it works fine. But, if i try to import a file saved on mac computer to a windows computer, i am unable to do so, but, however, if i open the same file in windows and save it again, it gets imported successfully.

Wat can be the issue?
Please suggest!


Another option is to read the file in using file_get_contents(), do a str_replace() sequence like this:

$data = str_replace("\\r\
", "\
", $data);
$data = str_replace("\\r", "\
", $data);

Then write the data back out to the file using file_put_contents(). Then you can use fgetcsv() without any issues.

Oh, Thanks, for quick replies, i’ll check it out!

Check out the auto_detect_line_endings configuration option, it may help somewhat.

When turned on, PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Macintosh line-ending conventions.

This enables PHP to interoperate with Macintosh systems, but defaults to Off, as there is a very small performance penalty when detecting the EOL conventions for the first line, and also because people using carriage-returns as item separators under Unix systems would experience non-backwards-compatible behaviour.

Line endings, I would guess. Windows uses \r
whereas *nix based systems normally use just

And to make it even more confusing Mac uses just \r.

Thanks guys, setting this parameter to 1 solved my issue.