1

I am using this Function I found here on stackoverflow

How to create and download a csv file from php script?

function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    $f = fopen('php://output', 'w');

    foreach ($array as $line) {
        fputcsv($f, $line, $delimiter);
    }

    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="'.$filename.'";');
}   

The Problem is in the export file i have now 4 extra empty lines.

How i can fix this function to prevent these extra lines.

Array ( 

[0] => Array ( [start] => 001-F15:B2:1-F [dest] => 002-F15:B2:5-Y [rres_1] => 100 [rres_2] => 100 [rres_3] => 100 [runit_1] => 0 [runit_2] => 0 [runit_3] => 0 [runit_4] => 0 [runit_5] => 0 [runit_6] => 0 [worker] => 0 [duration] => 12 ) 

[1] => Array ( [start] => 001-F15:B2:1-F [dest] => 005-F15:A5:3-M [rres_1] => 0 [rres_2] => 0 [rres_3] => 0 [runit_1] => 100 [runit_2] => 100 [runit_3] => 100 [runit_4] => 0 [runit_5] => 0 [runit_6] => 0 [worker] => 0 [duration] => 12 ) 

[2] => Array ( [start] => 006-F15:E4:2-Y [dest] => 004-F15:A5:1-Y [rres_1] => 0 [rres_2] => 0 [rres_3] => 0 [runit_1] => 0 [runit_2] => 0 [runit_3] => 0 [runit_4] => 100 [runit_5] => 100 [runit_6] => 100 [worker] => 100 [duration] => 12 ) 
) 

This lines are printed out in the export.csv correctly

now the complete code:

function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="'.$filename.'";');

    // open the "output" stream
    // see http://www.php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
    $f = fopen('php://output', 'w');

    foreach ($array as $line) {
        fputcsv($f, $line, $delimiter);
    }
}   
$sql='SELECT p1.planet_name AS start, p2.planet_name AS dest, rres_1, rres_2, rres_3, runit_1, runit_2, runit_3, runit_4, runit_5, runit_6, worker, duration FROM traderoute tr 
        INNER JOIN planets p1 ON p1.planet_id=tr.start_planet
        INNER JOIN planets p2 ON p2.planet_id=tr.dest_planet
        WHERE player_id='.$game->player['user_id'];
$export=$db->queryrowset($sql); 
array_to_csv_download($export);
die();
3
  • Are these extra lines at the beginning, at the end, scattered among the other lines? Commented Feb 1, 2014 at 10:00
  • If they're at the beginning, is there anything in your code prior to calling this function that outputs anything? echo or print statements, or blank lines outside of PHP tags? Seeing that the first few entries in your array contains data increases the likelihood of this being the cause Commented Feb 1, 2014 at 10:39
  • no only empty lines inside the PHP TAG but i removed them and get the same trouble. Commented Feb 1, 2014 at 10:51

2 Answers 2

2

I needed to clean the lines after the closing PHP ?> tag in the included PHP files. Then the empty lines in the csv file were gone.

Sign up to request clarification or add additional context in comments.

Comments

-1

Try checking for a blank line like this:

foreach ($array as $line) {
   if(trim($line)!="")
   {
      fputcsv($f, $line, $delimiter);
   }
}

6 Comments

Surely if(!empty(trim($line))) {...} would cover more possibilities and be cleaner
PHP Fatal error: Can't use function return value in write context on Line 31 and Line 31 = if(!empty(trim($line)))
i changed now to code foreach ($array as $line) { $line=trim($line); if(!empty($line)) { fputcsv($f, $line, $delimiter); } now my export file is empty
Try if(trim($line)!="")
output now is empty i get the array data from SQL database, i post now my array in the original post
|

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.