Saving data to CSV.

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
mariostg
Forum Members
Forum Members
Posts: 12
Joined: Wed Apr 08, 2020 12:10 pm

Saving data to CSV.

Post by mariostg »

I have this very basic function that saves records as csv, but when it saves, it also inlcudes the page content, header, etc. There has to be a trick, probably stupid, but I cannot figure it out.

Code: Select all

    public function toXL(){
        $this->GetMatches();
        echo $this->_totalmatchingrows. "Lines found<br>";
        $filename = "registraire_" . date('Ymd_HMS') . ".csv";
        
        $out = fopen("php://output", 'w');
        $this->MoveFirst();
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Content-Type: text/csv; charset=UTF-8");
        while(!$this->EOF()){
            $row = $this->fields;
            $retval = fputcsv($out, array_values($row), ',', '"');
            //echo "<br>Wrote ". $retval.'<br>';
            $this->MoveNext();
        }
        fclose($out);
    }
SwBd
New Member
New Member
Posts: 6
Joined: Mon May 13, 2019 5:52 pm

Re: Saving data to CSV.

Post by SwBd »

Hello,

just add

Code: Select all

exit;
at the very end
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

Re: Saving data to CSV.

Post by Jo Morg »

You'll need to clear the output buffers from any stored output right before sending the headers, and then kill the process right after the output to prevent further content being printed to the buffers.

Code: Select all

    public function toXL(){
        $this->GetMatches();
        echo $this->_totalmatchingrows. "Lines found<br>";
        $filename = "registraire_" . date('Ymd_HMS') . ".csv";
       
        $out = fopen("php://output", 'w');
        $this->MoveFirst();
       /***************************************/
       $handlers = ob_list_handlers();
       for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
       /***************************************/
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Content-Type: text/csv; charset=UTF-8");
        while(!$this->EOF()){
            $row = $this->fields;
            $retval = fputcsv($out, array_values($row), ',', '"');
            //echo "<br>Wrote ". $retval.'<br>';
            $this->MoveNext();
        }
        fclose($out);
       die; // exit or die here to prevent further output to the buffer
    }
 
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
mariostg
Forum Members
Forum Members
Posts: 12
Joined: Wed Apr 08, 2020 12:10 pm

[SOLVED] Re: Saving data to CSV.

Post by mariostg »

Jo Morg.
Thanks. That solved the problem.
Locked

Return to “CMSMS Core”