0

I have built an application which takes a csv file and processes it, saving all the interesting bits to my database.

The source of the csv file is an excel file which I have to download from a 3rd party website. In order to make it csv is simply save it as so in open office and bobs your uncle.

So when I do this from either my windows or linux machine, the files I create upload as expected no bother, however when my colleague does similar on his mac, then we have problems.

I know there are some differences between the the formats of the csv but surely once you've put the file through

$csvData = file_get_contents($file);
$lines = explode(PHP_EOL, $csvData);
$arrayOfLinesAndCols = array();
foreach ($lines as $line) {
   $arrayOfLinesAndCols[] = str_getcsv($line);
}

then the output should be the same. The files we are both uploading 'look' the same when you open them.

  • So what would those problems look like?Is the data identical? – Mihai Apr 15 '16 at 16:05
  • Why don't you let your colleague give you the file and analyze it? – Gerald Schneider Apr 15 '16 at 16:08
  • 1
    It probably depends. `PHP_EOL` is a representation of the default `end of line` character for _that particular system it is running on_, but if the file is made on another system it won't match. If open office always uses a windows return, the file will look the same (as macs are made to be interoperable), but the end of line character is different on each system... – somethinghere Apr 15 '16 at 16:09
  • What exactly is the problem you're having? And the differences I can think of are: encoding (when saving), delimiter, string delimiter and of course end-line characters. – Technoh Apr 15 '16 at 16:09
  • You need to run the script on the OS that saved the `.csv` file as PHP_EOL means `0D 0A` on windows and just `0D` on UNIX and some MAC's use something else completely and not all MAC use the same – RiggsFolly Apr 15 '16 at 16:11
  • Unix/Linux uses `0A` and Mac uses `0D` for end-of-line. Windows uses `0D 0A`. – Kelly Keller-Heikkila Apr 15 '16 at 16:12
  • Yea but you get my point though – RiggsFolly Apr 15 '16 at 16:15
  • Why don't you use fgetcsv() in the first place; with the `auto_detect_line_endings` setting as described in the [PHP Docs](http://www.php.net/manual/en/function.fgetcsv.php) – Mark Baker Apr 15 '16 at 16:17
  • You might like to read this. The `dos2unix` and `unix2dos` utils might be useful as a preprocessor http://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-bash-script Of course @MarkBaker suggestion beats us all into the dust – RiggsFolly Apr 15 '16 at 16:17

0 Answers0