Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get return value of fputcsv

Tags:

php

csv

Does anyone know of a function, or a cool trick, to get the return value of fputcsv instead of writing the output to file?

I'm trying to encode a file as a tab-delimited file, but my client wants one of the columns encoded in csv (so 1 column of csv values in a tab delimited file). Not ideal, I agree, but at this point I just need to get it done.

Thanks!

like image 702
Jim Rubenstein Avatar asked Sep 09 '11 13:09

Jim Rubenstein


2 Answers

Shamelessly copying from a user note on the documentation page:

$buffer = fopen('php://temp', 'r+');
fputcsv($buffer, $data);
rewind($buffer);
$csv = fgets($buffer);
fclose($buffer);

// Perform any data massaging you want here
echo $csv;

All of this should look familiar, except maybe php://temp.

like image 74
Jon Avatar answered Oct 16 '22 09:10

Jon


$output = fopen('php://output', 'w');
fputcsv($output, $line);
fclose($output);

Or try one of the other supported wrappers, like php://memory if you don't want to output directly.

like image 3
deceze Avatar answered Oct 16 '22 09:10

deceze