Question

I am trying to output data from an array to a csv and download it.

I am using social engine which is built on zend framework.

public function indexAction()
    {
        $this->outputCSV();
        //$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('passport_admin_main', array(), 'passport_admin_main_outofarea');
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        header('Content-Disposition: attachment; filename="OutOfAreaReport.csv"');
        //content type
        header('Content-type: application/excel');
        //read from server and write to buffer
        readfile('spreadsheet/OutOfArea.csv');
    }

    public function outputCSV(){
        $list = array (
                    array('aaa', 'bbb', 'ccc', 'dddd'),
                    array('123', '456', '789'),
                    array('"aaa"', '"bbb"')
                );
        $fp = fopen('OutOfAreaReport.csv', 'w');

        foreach ($list as $fields) {
            fputcsv($fp, $fields);
        }

        fclose($fp);
    }
}

At the moment it downloads the CSV but it has no values in the CSV it is empty.

Was it helpful?

Solution

You're using two different paths:

    readfile('spreadsheet/OutOfArea.csv');
              ^^^^^^^^^^^^

    $fp = fopen('OutOfAreaReport.csv', 'w');
                 ^----no path
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top