0

I am using fputcsv to create a csv but the data it outputs starts on the 3rd row when i open it in excel. Why is this?

I want to create a row of column headers using fputcsv, what is the best way to do this.?

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"');
        header('Content-type: application/excel');
        readfile('OutOfAreaReport.csv');
    }

    public function outputCSV(){
        $list = array (
                array('aaa', 'saasasbbb', 'ccdddc', 'dddd')
                );
        $fp = fopen('php://output', 'w');

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

        fclose($fp);
    }
RSM
  • 14,540
  • 34
  • 97
  • 144

2 Answers2

5

Because you are writing the data to php://output, I assume you are offering the file for download?

Well, the extra new lines in the file are coming from further up your script, you have already output them.

The most common culprit here is leading white space before the opening <?php tag in your script:

Wrong:




<?php
  // Your code here

Right:

<?php
  // Your code here

EDIT Thanks to @SDC for reminding me that this also applies to any files you have included. For these files you will also need to make sure there is no trailing white space after the closing ?> tag, or preferably remove the closing tag completely, as it is not necessary for the script to work correctly.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 1
    +1. Note that this applies to other PHP files you may have included as well as the main one. And you also need to make sure you don't have any blank lines at the end of the files as well as at the begining. – SDC Aug 15 '12 at 16:27
  • @SDC Good point about included files, forgot about that, will add a note to the main answer body. – DaveRandom Aug 15 '12 at 16:29
  • This makes the CSV start on line 2 now instead of 3. Is is the case that it reserves this for column headers or? – RSM Aug 16 '12 at 08:44
  • 2
    @RyanMurphy Sounds like you still have some new lines leaking it there somewhere, check *all* your PHP scripts that are required to make the file for this problem (every file that has been `include`/`require`d). Also make sure you don't have any gaps anywhere in the file between `?> ` from the file completely). Also make sure you don't have any calls to `echo`, `print` etc that might be outputting anything before this point in your script. – DaveRandom Aug 16 '12 at 09:03
5

I know this has been marked as answered but when I ran into a similar problem I had a lot of includes so I added ob_clean; to clear the buffer and it solved the space issue. Example:

public function outputCSV(){

$list = array ( array('aaa', 'saasasbbb', 'ccdddc', 'dddd') );

//add here  
ob_clean;
   $fp = fopen('php://output', 'w');

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

   fclose($fp);

}

TomBene
  • 101
  • 2
  • 2