0
    $csvFile = [];
    $firstRow = ['#','Freq','Mode','Shift','Offset','TX Freq','Enc/Dec','Tone','Code','Show','Name','Power','Scan','Clk','Step','Scan2','Scan3','Scan4','Scan5','Scan6','Description','Bank1','Bank2','Bank3','Bank4','Bank5','Bank6','Bank7','Bank8','Bank9','Bank10','Bank11','Bank12','Bank13','Bank14','Bank15','Bank16','Bank17','Bank18','Bank19','Bank20','Bank21','Bank22','Bank23','Bank24','PRFRQ','SMSQL','RXATT','BELL','Masked','Internet','DCSinv'];
    array_push($csvFile, $firstRow);

    while($row = mysqli_fetch_array($result)) {
    $element = [count($csvFile), $RX, $modulation, $offsetDirection, $offsetFrequency, '', $toneMode, $CTCSS, '023', '', $row['Repeater'].'-'.$row['Sign'], 'HIGH', 'Off', 'OFF', "12.5 K", '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '1600HZ', 'OFF', 'OFF', 'OFF', 'NO', 'OFF', 'RN-TN'];
    array_push($csvFile, $element);
    }

    function array_to_csv_download($array, $filename = "FTBVX8DE.csv", $delimiter=";") {
    $f = fopen('php://memory', 'w'); 
    foreach ($array as $line) { 
        fputcsv($f, $line, $delimiter);
    }
    fseek($f, 0);
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'";');
    fpassthru($f);
    }

    array_to_csv_download($csvFile);

Hi guys, I do need your help. Above is a snippet of code that creates a CSV file to import data in a specific software for ham radio operators. Apparently if I open the file in Excel, the output looks correctly, with corresponding fields/values in columns.

But when I try to import data in the software, an error occurs. Opening the CSV file in Notepad I noticed that my data were shown straight into a single line: wrong formatting without a pseudo carriage return

Instead, the formatting for a succesfull import should be:

correct formatting must be exactly like this

Now, I tried several ways to accomplish this task searching in StackOverflow, for instance inserting PHP_EOL, but unsuccessfully.

Any help/suggestion, please? Thank you so much!

user7179753
  • 29
  • 1
  • 7
  • The PHP documentation for `fputcsv` has a note that says: "Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem." Would that help you at all? I don't think that inserting a PHP_EOL as a field will work, since fputcsv would contain that in the enclosure characters. – RToyo Jun 09 '17 at 17:07
  • Does the same thing happen when you write it to an actual file on disk, instead of into memory? Have you checked that your array contains the "correct" data in the first place? – CBroe Jun 09 '17 at 17:10
  • @Robbie Toyota, this is an attempt I already tried unsuccessfully, both enabling it in the php.ini file and inserting ini_set('auto_detect_line_endings',true); at the beginning of the php file. – user7179753 Jun 09 '17 at 17:43
  • @CBroe Yep, unfortunately. Data are correct, as I mentioned before they look nicely when opened in Excel or in Notepad. The only incorrect thing is the failure of line endings – user7179753 Jun 09 '17 at 17:50
  • What, you said before that it _didn't_ "look nice" when opened in Notepad? And if that means the standard Windows Notepad, this would indicate that indeed the line break character is the problem - because Windows Notepad is known to fail to recognize line breaks that are not in "Windows format", meaning `\r\n`. So likely your CSV file contains just `\n`, which Excel seems to handle fine - but the software you import this into seems to be as dumb as Notepad ;p Try explicitly replacing all `\n` with `\r\n`, and see if that changes things. – CBroe Jun 09 '17 at 18:05
  • @CBroe Sorry, it was a distraction: data look nice in Excel only. I never used \n. Anyway, using \r\n gives a slight improvement: there is a sort of carriage return, but last field and first one in the next line are linked with double quotes, like this: ...;"DCSinv (CR) "1;145,61250 .... and this is still not accepted by the software. I use Windows Notepad both for the wrong and correct format. And finally, yes, this softare is dumb! – user7179753 Jun 09 '17 at 18:19
  • 1
    https://stackoverflow.com/questions/4080456/fputcsv-and-newline-codes has a few suggestions how to use a different line terminator while keeping fputcsv in place ... check if one of those approaches helps maybe. – CBroe Jun 09 '17 at 18:26
  • @CBroe met with failure: same result as for simply appending PHP_EOL to the last array element, i.e. insertion of double-quotes like: ... ... ... ... ;"DCSinv(CarriageReturn)"1;145,61250 .... ... ... ... – user7179753 Jun 10 '17 at 06:55

0 Answers0