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);
    }
3
  • exit($output); should print Resource id #xx as expected. So what is your question? Commented Nov 23, 2012 at 17:10
  • I want to virtually create a file, write it and provide it for download such that file should not be save on server. Commented Nov 23, 2012 at 17:17
  • 1
    Like this php.net/manual/en/function.fputcsv.php#106583? Commented Nov 23, 2012 at 18:04

3 Answers 3

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) );
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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);

4 Comments

I am completely not clear with link you provided for solution. Can you rewrite it please. My data array is ($customerListAll)
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.
It's writing HTML of my page.
It's writing HTML of my page to the CSV FILE.
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>";


}

Comments

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.