4

I am trying to export database data into .csv via php. After a certain length of data it doesn't save the query into a .csv file but displays it on the screen. It looks like it's independent on which records are queried, and it also seems to be independent of any special characters.

$handle = fopen("php://output", "w");
fputcsv($handle, array('Name','Text','Link','Category','Price','Package', 'Date of upload','Date of verification','Date of expiry','Status','Clk'));
for ($c=0; $c<$num_rows; $c++)
{
    $row2[$c][0] = iconv("UTF-8","WINDOWS-1257",html_entity_decode( $row2[$c][0] ,ENT_COMPAT,'utf-8'));
    $row2[$c][1] = iconv("UTF-8","WINDOWS-1257",html_entity_decode( $row2[$c][1] ,ENT_COMPAT,'utf-8'));
    fputcsv($handle, array($row2[$c][0], $row2[$c][1], $row2[$c][2], $row2[$c][3], $row2[$c][4], $row2[$c][5], $row2[$c][6], $row2[$c][7], $row2[$c][8], $row2[$c][9], $row2[$c][10]));
}
fclose($handle);

header('Content-Type: text/csv; utf-8');
header("Content-Disposition: attachment; filename=".$filename);
header("Pragma: no-cache");
header("Expires: 0");

11 columns, 18 records. With 19 records it's not working.

Am I missing some setting?

erdomester
  • 11,789
  • 32
  • 132
  • 234

3 Answers3

3

Place the headers before the output!


If you convert to utf use:

iconv("WINDOWS-1257", "UTF-8", ...

The order of arguments was wrong

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The header was the problem. I am not using `php://stdout`, I am using `php://output`. With `php://stdout` I get an empty file. Also what is wrong with my utf conversion? – erdomester Aug 10 '13 at 11:40
  • 1
    You convert **to** Windows CP-1257 but setting an utf8 header – hek2mgl Aug 10 '13 at 11:42
  • 1
    Using `php://output` is perfectly fine, especialy when working with functions that operate on streams. Can you elaborate why in your opinion it does not make sense? – dev-null-dweller Aug 10 '13 at 11:48
  • No, `join` would do the job for very limited set of data. Some data should be put in an encloser and some of the characters escaped to produce valid CSV. – dev-null-dweller Aug 10 '13 at 12:01
  • @dev-null-dweller I have admitted this (by removing that part of the comment) – hek2mgl Aug 10 '13 at 12:01
  • So what now? After replacing the headers to the top and using iconv the right way, does the problem still exist? – hek2mgl Aug 10 '13 at 12:07
  • No. Like I said, the header was the problem. I had to put it before output. – erdomester Aug 10 '13 at 12:29
  • Ok. After all I admit - again - that the fputcsv action together with php://output was definitely ok. my fault! ;) – hek2mgl Aug 10 '13 at 12:31
  • @hek2mgl Why do you say I am converting to Windows CP-1257? – erdomester Aug 10 '13 at 12:32
  • because it should be `iconv($from, $to,..)` http://www.php.net/manual/en/function.iconv.php – hek2mgl Aug 10 '13 at 12:34
2

Alternatively to the great answer by @hex2mgl 'set headers before fopen()', you could use output buffering to prevent fputcsv from printing directly to the screen instead of into the handle:

ob_start();
$out = fopen('php://output', 'w');
$csv = ob_get_clean();
fclose($out);

Update:

Just found that the new PHP i/o php://temp does a much better job and doesn't require output buffering. Uses memory first (upto 5mb or whatever you specify), then will switch to disk if needed:

$fiveMBs = 5 * 1024 * 1024;
$stream = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
J W
  • 2,801
  • 2
  • 18
  • 22
1

Here:

$handle = fopen("php://output", "w");

You are getting the handle for the standard output (witch is kind of a file).

Guess you meant:

$handle = fopen($filename, "w");
fsw
  • 3,595
  • 3
  • 20
  • 34
  • This way I get an empty .csv file. – erdomester Aug 10 '13 at 11:35
  • if you want fputcsv to print to stdout then you question is very misleading. you want browser to save it to *.csv not php – fsw Aug 10 '13 at 12:00
  • "I am trying to export database data into .csv" - Why is that misleading? – erdomester Aug 10 '13 at 12:30
  • this: "fputcsv prints result to screen instead into csv", you want fputcsv to print out its results, you don't want it to write to file, you just want your browser to save server response to file instead of showing it, PHP is server side language. – fsw Aug 10 '13 at 13:04