3

please I have the following array :

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "lkjhgj"
    [1]=>
    string(16) "[email protected]"
  }
  [1]=>
  array(2) {
    [0]=>
    string(5) "hgjk,"
    [1]=>
    string(18) "[email protected]"
  }
  [2]=>
  array(2) {
    [0]=>
    string(9) "dddd ffff"
    [1]=>
    string(13) "[email protected]"
  }
}

I want to put it into a csv file, so I've tried :

$fichier = 'file.csv';
$fp = fopen($fichier, 'w');

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

fclose($fp);

header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$fichier);

But when I download the file I found it empty !

Please masters any idea ? Thanks in advance

PS : Permissions are 777

3
  • 2
    Where does $list come from in your code example? What's in there? Commented Feb 11, 2013 at 9:25
  • 1
    I don't see any code that actually returns the file as part of the response! After you've closed the file it's got to be reopened before you can send it. Also I don't see any error checking in your code, fopen, fputcsv and other file operations can fail. You need to check that they've not returned FALSE Commented Feb 11, 2013 at 9:25
  • use $fp= fopen('php://output', 'w'); instead of $fp = fopen($fichier, 'w');.. see my answer below Commented Feb 11, 2013 at 9:28

3 Answers 3

14
 $fichier = 'file.csv';
 header( "Content-Type: text/csv;charset=utf-8" );
 header( "Content-Disposition: attachment;filename=\"$fichier\"" );
 header("Pragma: no-cache");
 header("Expires: 0");

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

 foreach ($list as $fields) 
 {
    fputcsv($fp, $fields);
 }
 fclose($fp);
 exit();
2
  • I tried this code. I am getting a corrupted csv file. Could not open the file. Please help. Commented Mar 11, 2014 at 19:31
  • Please include an educational explanation to this code-only answer. Commented Sep 23, 2018 at 11:14
0

If you are saving array in session and issues of encoded characters.

session_start();
$fp = fopen($_SESSION['CSV']['type'].'.csv', 'w');
foreach ($_SESSION['CSV'] as $fields) {
    $val1 = htmlspecialchars_decode(utf8_decode(html_entity_decode($fields[0], ENT_QUOTES | ENT_XML1, 'UTF-8')));
    $val2 = htmlspecialchars_decode(utf8_decode(html_entity_decode($fields[1], ENT_QUOTES | ENT_XML1, 'UTF-8')));
    $val3 = htmlspecialchars_decode(utf8_decode(html_entity_decode($fields[2], ENT_QUOTES | ENT_XML1, 'UTF-8')));
    fputcsv($fp, array($val1,$val2,$val3,$val4,$val5));
}
fclose($fp);
0

if somebody is looking for exporting the results from a query with dynamic headers to an array then to csv file you could use the following function :

function exportCSVFile($fieldsNames, $result)
{

$fileName = "result.csv";
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$fileName");
header("Pragma: no-cache");
header("Expires: 0");
$stdout = fopen('php://output', 'w');

fputcsv($stdout, $fieldsNames, ',', '"'); // put the headers or fields Names

$resultArray = array();

$tempRowHolder = array();

/* 
    build the finalized array which will be used to export the result as csv 
*/

for ($i = 0; $i < count($result); $i++) {
    for ($j = 0; $j < count($fieldsNames); $j++) {
        $tempRowHolder[$j] = $result[$i][$fieldsNames[$j]]; // fetch a row by the different fields names
    }

    /* push the row from the tempRowHolder array to the main array($resultArray) */
    array_push($resultArray, $tempRowHolder);

    /* clear the temporary array (the holder of the row) to use it fresh in the next iteration */
    $tempRowHolder = [];
}

$i = 0;

/* put the finalized array into the csv file  */
while ($i < count($resultArray)) {
    fputcsv($stdout, $resultArray[$i++]);
}

fclose($stdout);
}

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.