2

I am passing an array from Javascript to PHP using AJAX and attempting to write the array as a new line in a CSV file. This row is being appended correctly but isn't including the necessary closing "," at the end.

$row = $_POST['row'];
$handle = fopen('filename.csv', 'a');
fputcsv($handle, $row);
fclose($handle);

I've tried messing with auto_detect_line_endings but to no success.

1 Answers1

2

A final , is not part of the CSV spec at all, unless the last field is null or empty.

Also, fputcsv() expects an array of fields. Is $_POST['row'] actually an array of fields? You may need to translate your POSTDATA into an appropriate array.

Community
  • 1
  • 1
Will
  • 24,082
  • 14
  • 97
  • 108