That sure won't work. Fwrite signature is
int fwrite(resource, string, int);. There is no $columns type option, that would be a single line within it.
You should be using
fwrite($handle, $csvData);, and either omitting the third parameter or giving it a maximum length to work with. So simply write the $columns to the file first, then write the data. Looks like you probably only want the columns if the file is empty so you can always check the size first.
fputcsv is an alternative. It was designed to create csv data from an array. You may want to consider that option instead.