1

When writing .csv files i use fputcsv like this:

- open a temporary file $f = tmpfile();
- write content to file using fputcsv($f,$csv_row);
- send appropriate headers for attachment
- read file like this:

# move pointer back to beginning 
rewind($f);

while(!feof($f))
    echo fgets($f);

# fclose deletes temp file !
fclose($f);

Another aproach would be:

- open file $f = fopen('php://output', 'w');
- send appropriate headers for attachment
- write content to file using fputcsv($f,$csv_row);
- close $f stream

My question is: What would be the best approach to output the data faster and taking into account server resources ?

First method would use more writes and consume more resources but would output very fast.

Second method uses less writes and would output slower i think.

Eagerly waiting for your opinions on this.

Thanks.

2
  • 1
    This is not about opinions. We don't think one or the other is faster. Try benchmarking both approaches, and you'll know which one is faster. Commented Aug 31, 2012 at 8:24
  • @BerryLangerak Yes, you are right. I'll try benchmarking this and post results. Commented Aug 31, 2012 at 8:29

2 Answers 2

0

fpassthru() will do what you're doing at a lower level. Use it like this:

# move pointer back to beginning 
rewind($f);

while(fpassthru($f) !== false);

# fclose deletes temp file !
fclose($f);

Even though it may be a csv file, there is no need to restrict yourself to csv functions, unless you are generating the file at the time of output.

You could probably see a performance gain if you stream the CSV to output instead of to a file.

Sign up to request clarification or add additional context in comments.

1 Comment

I am generating a large .csv file from the database. It's always faster to use built-in functions offered by php. Thanks for fpassthru() ! I learned something today :).
0

Why do you need to write the csv content to a tmp file/php's output stream ?

You just need to echo the csv content directly, there should not be any file operations.

  • send appropriate headers for attachment
  • echo the csv content.

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

foreach ($csv_rows as $csv_row) {
  echo $csv_row;
}
exit;

7 Comments

Because he uses fputcsv( ) to transform an arbitrary array to a CSV string. There is no stringtocsv function in PHP, afaik.
If $csv_row is an array, then you just need to implode it with the delimiter. echo implode(',', $csv_row)."\n";.
Actually, the CSV is wildly complex, and you're grossly underestimating it. For example, delimiters have to be escaped, as do the separator. If a value from an array is "foo, bar, baz", the implode idea will generate a broken CSV file. It's tougher than you think.
@BerryLangerak I know that. But you could always do the escape yourself, which is not complicated thing.
Yes, it is a complicated thing. But by all means; do try.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.