0

I am trying to export CSV using PHP. But instead of printing the result it's printing Resource id #26 in the generated CSV FILE. If I remove exit from end it print's my whole HTML page content. My code is...

      if (isset($_REQUEST['download']) && ($_REQUEST['download'] == "yes")) {
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=link_point_report.csv');
            $output = fopen('php://memory', 'w+');
            fputcsv($output, array('Member Id', 'Customer Name', 'Customer Email'));

            $customerListAll = $oAdmFun->linkpoint_check_customer('', '');       
             $oAdmFun->pr($customerListAll, true);
//                if (count($customerListAll) > 0) {
//                        for ($c = 0; $c < count($customerListAll); $c++) {
//                                fputcsv($output, array('Sankalp', 'Sankalp', 'Sankalp'));
//                        }
//                }

            ob_clean();
            flush();
            exit($output);
    }
Sankalp
  • 1,300
  • 5
  • 28
  • 52

3 Answers3

3

That's because $output is a resource, which is what fopen() returns. You need to use fread() to get its contents.

EDIT: Now I understand what you were asking. To output the contents of your CSV, you need to get the text output from the resource:

rewind( $output );
$csv_output = stream_get_contents( $output );
fclose( $output );
die( $csv_output );

And it's also a good idea to set the content-length header so that the client knows what to expect:

header('Content-Length: ' . strlen($csv_output) );
Community
  • 1
  • 1
0x6A75616E
  • 4,696
  • 2
  • 33
  • 57
  • I want to virtually create a file, write it and provide it for download such that file should not be save on server. I think you didn't got my question. – Sankalp Nov 23 '12 at 17:17
0

Open a chunk of writable memory:

$file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+'); // 128mb

Write to it... then fetch the contents with:

rewind($file);
$output = stream_get_contents($file);
fclose($file);
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • I am completely not clear with link you provided for solution. Can you rewrite it please. My data array is (`$customerListAll`) – Sankalp Nov 23 '12 at 18:11
  • No, that's your data and I don't need to know the data structure. The code above shows how to open a file resource to a temporary location in memory. Once you have $file you can write to it with `fputcsv($file, ..fields..)`. After you've written all the information grab the contents of said file using the rewind and stream_get_contents functions. – Mike B Nov 23 '12 at 18:19
  • It's writing HTML of my page. – Sankalp Nov 23 '12 at 18:22
  • It's writing HTML of my page to the CSV FILE. – Sankalp Nov 23 '12 at 18:28
0
function CSV_HtmlTable($csvFileName)
{
// better to open new webpage 
echo "<html>\n<body>\n\t<table style=''>\n\n";
$f = fopen($csvFileName, "r");
$trcount = 0; //start the row count as 0
while (($line = fgetcsv($f)) !== false) {
        $trclass = ''; if ($trcount%2==0) { $trclass=' class="dark"'; } //default to nothing, but if it's even apply a class
        echo "\t\t<tr".$trclass.">\n"; //same as before, but now this also has the variable $class to setup a class if needed
        $tdcount = 0; //reset to 0 for each inner loop
        foreach ($line as $cell) {
                $tdclass = ''; if ($tdcount%2==0) { $tdclass=' class="dark"'; } //default to nothing, but if it's even apply a class
                echo "\t\t\t<td ".$tdclass."style='padding:.4em;'>" . htmlspecialchars($cell) . "</td>"; //same as before, but now this also has the variable $class to setup a class if needed
                $tdcount++; //go up one each loop
        }
        echo "\r</tr>\n";
        $trcount++; //go up one each loop
}
fclose($f);
echo "\n\t</table>\n</body>\n</html>";


}
Khan
  • 151
  • 1
  • 4