0

I have a script that writes a .CSV ,pretty self explaining:

$handle = fopen($name);

foreach ($data as $tkey => $tval) {
    fwrite($handle, $eol);
}
fclose($handle);

This works compleatly fine, everything is, as it should be.

BUT: At the end of the file, there is always a blank line that causes problems, when i want to process the .CSV with another programm. EX:

1 Date;Time
2 22.4;03:22;
3 23.4;09:22;
4 24.4;14:22;
5

When I reload the file and echo it to the browser, the line is not within the array, so it seems to be created, when the file is closed.

I have read aboud some sort of of PSR Coding guides, but thought these are more like ,yeah, guidelines. Is the blank line a fixed thing or is there a a workaround?

Martin
  • 22,212
  • 11
  • 70
  • 132
  • 1
    Use `fputcsv` instead of `fwrite` – DarkBee Apr 29 '21 at 08:53
  • A word of warning for any non-simple data -- `fputcsv` this function can be problematic. It's one of PHP's least good native functions. – Martin Apr 29 '21 at 08:55
  • What data is in `$eol`? – Martin Apr 29 '21 at 08:56
  • @DarkBee: Sorry, mistake on my side, i cannot use fputcsv, because the method for writing is something that is predetermined by a collegue of mine. I just broke down the method to something readable. (His writing is......complicated....) –  Apr 29 '21 at 09:00
  • @Vetri why are you writing an end of line character (only!) to a file? Your code does not make sense and doesn't correlate with your claimed output problem. Your code for `fopen` looks wrong (no modes specified) and your code for `fwrite` also looks wrong as you're simply writing a blankspace which is the exact issue you have. Also your `foreach` is not using any of the values within the foreach loop. – Martin Apr 29 '21 at 09:05

0 Answers0