2012-08-15 80 views
0

我想从数组输出数据到csv并下载它。将数组输出到csv

我使用基于zend框架的社交引擎。

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

目前它下载CSV,但CSV中没有值是空的。

+0

任何在你的错误日志? – wgcrouch 2012-08-15 15:34:47

+0

不幸的是不是 – RSM 2012-08-15 15:43:58

+0

你是否直接在服务器上检查过文件? – 2012-08-15 15:43:58

回答

3

您使用两个不同的路径:

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

    $fp = fopen('OutOfAreaReport.csv', 'w'); 
       ^----no path 
+1

这是CrowdDebugging的一个很好的例子:)而不是CroudSourcing – 2012-08-15 15:50:40