2

I'm attempting to capture the output of fputcsv() in order to use gzwrite() to actually write to a tab-delimited file. Basically, I'm querying a database and I want to put these rows into a gzipped CSV file and I'd rather use fputcsv() than actually append "\t" and "\n" everywhere. Can I somehow do this with output buffering or something similar?

Here's the basic outline of what I have:

$results = get_data_from_db();
$fp = gzopen($file_name, 'w');
if($fp) {
    foreach ($results as $row) {
        ???//something with gzwrite() ?
    }
    gzclose($fp);
}

Thanks!

EDIT: My understanding was that gzwrite() needs to be used to actually write to the file in order for it to actually be gzipped - is this not correct?

4 Answers 4

17

As read on php.net, you could try this little trick:

<?php
// output up to 5MB is kept in memory, if it becomes bigger
// it will automatically be written to a temporary file
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');

fputcsv($csv, array('blah','blah'));

rewind($csv);

// put it all in a variable
$output = stream_get_contents($csv);
?>

This is actually what you tried to avoid (capturing the output), but capturing is a very powerful tool, so why not use it?

4
  • 1
    I didn't know you could do that... interesting. (rubs chin) Jan 6, 2009 at 19:55
  • Forcing data in memory to write to a temp file may reduce ram usage but slow the app down, use with caution.
    – TravisO
    Jan 8, 2009 at 21:31
  • 1
    Can you comment on why you used rewind()?
    – arnaslu
    Nov 24, 2011 at 10:58
  • 2
    When you put the contents into the file, its pointer points to the end of the file. With rewind() you reset the file pointer to the beginning of the file, which allows you to read the full content from the beginning up to EOF.
    – Dan Soap
    Nov 26, 2011 at 18:45
3

Maybe I'm misunderstanding the question but fputcsv() operates on a file handle, which is what gzopen returns, so you can do this:

$results = get_data_from_db();
$fp = gzopen($file_name, 'w');
if($fp) {
    foreach ($results as $row) {
        fputcsv($fp, $row);
    }
    gzclose($fp);
}

I've tested this and it works fine.

2
  • 1
    Do you not need to use gzwrite() to write to the file correctly? Jan 6, 2009 at 20:00
  • I couldn't get it to display properly at first (unrelated bug), but, yep - apparently fputcsv() by itself is sufficient. Jan 6, 2009 at 21:03
1

You can do it by writing your own stream handler to intercept the writes to the underlying file.

Edit: After reading the spec example 3 on this page sounds like it might be of interest to you

1

Don't forget you can always do this manually without using the csv function without doing anymore work already. I had to do this recently so I could take the same data and either output a CSV as tab delimited, comma delimited or something a browser could display.

All you need to do is use basic arrays and implode (here's a basic example, you need to add some vars to support proper HTMl table output such as column head and tail, row head and tail, etc... but you get the idea:

switch ( $_GET['mode'] )
{
    case "csv":
        $wrapper = "'";
        $delimiter = ",";
        $nextrow = "\n";
        break;
    case "txt":
        $wrapper = "";
        $delimiter = "\t";
        $nextrow = "\n";
        break;
    case "html":
        $wrapper = "'";
        $delimiter = ",";
        break;
}

$output = array();
$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while ( $row = mysql_fetch_assoc($result) )
{
    $line = array();
    foreach ( $row as $column => $value )
    {
        $line[] = $wrapper . $value . $wrapper;
    }
    $output[] = implode("", $line) . $nextrow;
}

print implode("",$output);
1
  • 1
    This doesn't appear to cover the possibility of having delimiters within the strings themselves, or the fact that when double quotes exist in strings they should be preceded by another double quote to escape
    – scrowler
    Feb 23, 2015 at 22:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.