Home > Software engineering >  Export an empty CSV file when create CSV function on PHP
Export an empty CSV file when create CSV function on PHP

Time:12-03

i need help. now i'm creating direct download CSV file when press the export button from frontend. but i always got an empty csv file. inside the code, i also place the CSV file on specific folder. And it can display a correct CSV format

here is my code

//CSV output
$fileName = 'CSV-Export.csv';
$rows = [];
foreach ($lists as $key => $value) { 
    $rows[$key]['product_id']    = $value['product_id'];
    $rows[$key]['product_name']  = $value['product_name'];
    $rows[$key]['price']         = $value['price'];
} 

$columnNames = [
    'Product ID',
    'Product Name', 
    'Price'
];

header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=".$fileName);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: UTF-8"); 

$file = fopen("csv/".$fileName, "w"); 

fwrite($file, "Report Export"."\r\n");   
fwrite($file, " ". "\r\n");

fputcsv($file, $columnNames);
foreach ($rows as $row) { 
    fputcsv($file, [
        $row['product_id'],
        $row['product_name'], 
        $row['price']
    ]);
} 

fclose($file); 

what is wrong with my code. please help

CodePudding user response:

You need to check the fputcsv documentation the fields are an array of strings, you can change the code:

$rows = [];
foreach ($lists as $key => $value) { 
    $rows[$key]['product_id']    = $value['product_id'];
    $rows[$key]['product_name']  = $value['product_name'];
    $rows[$key]['price']         = $value['price'];
} 

to:

$rows = array();
foreach ($lists as $key => $value) { 
    $temp=array();
    $temp[]=$value['product_id'];
    $temp[]=$value['product_name'];
    $temp[]=$value['price'];
    $rows[]=$temp;
} 

CodePudding user response:

Update. Made it much more simple )

First things first ) You are creating file after sending headers and that's why you are receiving empty csv file.

Simplified code example with populated $lists array you can find below.

<?php
$fileName = 'CSV-Export.csv';
$lists = [
    [
        'product_id' => 1,
        'product_name' => "product_1",
        'price' => 150
    ],
    [
        'product_id' => 2,
        'product_name' => "product_2",
        'price' => 160
    ]
];
$columnNames = [
    'Product ID',
    'Product Name', 
    'Price'
];
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=".$fileName);
header('Cache-Control: must-revalidate');
header("Content-Transfer-Encoding: UTF-8"); 
echo "Report Export \r\n";
echo " \r\n";
echo  implode(",", $columnNames) . "\r\n";
foreach ($lists as $key => $value) { 
    echo $value['product_id'] . "," . $value['product_name'] . "," . $value['price'] . "\r\n";
}
exit(0);
?>

The other solution more closer to yours )

<?php
//CSV output
$fileName = 'CSV-Export.csv';
$lists = [
    [
        'product_id' => 1,
        'product_name' => "product_1",
        'price' => 150
    ],
    [
        'product_id' => 2,
        'product_name' => "product_2",
        'price' => 160
    ]
];
$columnNames = [
    'Product ID',
    'Product Name', 
    'Price'
];

$file = fopen("csv/" . $fileName, "w"); 
fwrite($file, "Report Export"."\r\n");   
fwrite($file, " ". "\r\n");
fputcsv($file, $columnNames);
foreach ($lists as $key => $value) { 
    fputcsv($file, [
        $value['product_id'], $value['product_name'], $value['price']
    ]);
}
fclose($file);

$stream = fopen("csv/" . $fileName, "r");
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=".$fileName);
header('Cache-Control: must-revalidate');
header("Content-Transfer-Encoding: UTF-8");
echo fread($stream, filesize("csv/" . $fileName));
exit(0);
?>
  • Related