So, my code generates a CSV file using PHP's built-in fputcsv function
my code
$delimiter = ';';
$filename = "members_" . date('Y-m-d') . ".csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('"FK"','"ID"', '"Name"', '"Email"', '"Phone"', '"Created"');
fputcsv($f, $fields, $delimiter, ' ');
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
// }
exit;
output :
FK;"ID";"Name";"Email";"Phone";"Created"
but i want like
"FK";"ID";"Name";"Email";"Phone";"Created"
please help me
Related
this code works on my offline version in xampp and automatically downloads CSV file. But the same code doesn't work on my live website in Hostgator. Is there something that i should change? it just displays on the browser instead of automatically generating/downloading the csv file
$delimiter = ",";
$filename = "depot_".$direction. date('Y-m-d') . ".csv";
// Create a file pointer
$f = fopen('php://memory', 'w');
// Set column headers
$fields = array( 'Depot Ref','Container No', 'Container Type', 'Check-In Date', 'Storage');
fputcsv($f, $fields, $delimiter);
// Output each row of the data, format line as csv and write to file pointer
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$storage=getStorage($row["depotRef"]);
$lineData = array( $row["depotRef"], $row['containerNo'], $row['containerType'],
$row['checkIn'], $storage);
fputcsv($f, $lineData, $delimiter);
}
// Move back to beginning of file
fseek($f, 0);
// Set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
exit;
I am exporting MySQL data successfully to a csv file. But in the csv file first line was empty and data starts from 2nd line. Below is my code.
$delimiter = ";";
$generated_date = date("Y-m-d-H-i-s");
$filename = 'order_detail_report'.$generated_date.".csv";
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '";');
$output = fopen('php://output', 'w+');
fputcsv($output, array('Order Type','Order Number','EAN','Article #'),$delimiter);
sql query
<?php
$lineData = array();
$query = "my select query";
$res = mysqli_query($mysqliConn,$query);
while($result= mysqli_fetch_assoc($res))
{
$lineData[] = $result;
fputcsv($output, $lineData, $delimiter);
unset($lineData);
}
Data was exporting successfully to csv but it was starting from 2nd line, any help would be greatly appreciated.
EDIT
<?php
//header info for browser
$delimiter = ";";
$generated_date = date("Y-m-d-H-i-s");
$filename = 'order_detail_report'.$generated_date.".csv";
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '";');
$output = fopen('php://output', 'w');
fputcsv($output, array('Order Type','Order Number','EAN','Article #','Title','Amazon Price','ERP Price','Requested Quantity','Dispatch Quantity','Rejected Quantity','Cancelled Quantity','Delivery Date','Status','EDD','LDD','Dispatch Center','Order Received','Price Status','Country'),$delimiter);
?>
I just removed all my mysql related query and only using the above one.
I have a problem with my export.
So I tried to export data to .csv with the following code :
$output = fopen('php://output', 'w');
$sFileName = 'test.csv';
header('Content-Disposition: attachement; filename="' . $sFileName . '";');
header("Content-Type:application/csv;charset=UTF-8");
fwrite($output, "sep=;\n");
fputcsv($output, array('Nom', 'Prenom'), ";");
foreach ($aFilterGifts as $value) {
fputcsv($output, $value, ";");
}
fpassthru($output);
fclose($output);
exit;
Where the $aFilterGifts is an array with data. But If I have in Nom, data like this Fossé, when I export I get FossГ©.
Is there a solution? Thank you in advance.
I found the error, it's necessairy to make $output = fopen('php://output', 'w'); after all headers,so thhe code is like this :
$sFileName = 'test.csv';
header('Content-Disposition: attachement; filename="' . $sFileName . '";');
header("Content-Type:application/csv;charset=UTF-8");
$output = fopen('php://output', 'w');
I am using this script to show a csv file:
if (isset($_GET['csv'])) {
header('Content-Type: text/csv; charset=utf-8');
$out = fopen('php://output', 'w');
foreach ($dataArray as $k => $v) {
fputcsv($out, $v);
}
fclose($out);
exit();
}
And this Javascript to open the csv for a direct download:
<a href="#" class="export" onclick="window.open(window.location.pathname + window.location.search + \'&csv=1\');">
Now I want to change the filename to something like:
subscriptions_2014_10_23.csv
Someone knows how to do so?
You need to add another header specifying the file name. Then throw in some date function to add into that:
$filename = 'subscriptions_' . date('Y_m_d') . '.csv';
header('Content-Disposition: attachment; filename="' . $filename . '"');
Just add one more header
header('Content-Disposition: attachment; filename=subscriptions_2014_10_23.csv');
maybe you should use header for file name :
header('Content-Disposition: attachment; subscriptions_2014_10_23.csv"');
So I have a simple script that writes some data to a CSV-file. The file contains a few non-ascii characters (norwegian characters) which are not displayed correctly in when opened in Excel. However they are displayed correctly in OpenOffice. Does anyone know how to fix this?
$fp = fopen('php://output', 'w');
if(!$fp)
{
echo "Could not write CSV-file"; die;
}
$filename = sprintf('%s_export_%s.csv', $marketplace, date('Y_m_d_H_i_s'));
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Pragma: no-cache');
header('Expires: 0');
foreach($collection as $i => $item)
{
$result = array();
$result[] = $item->getData('email');
$result[] = $item->getData('firstname');
$result[] = $item->getData('lastname');
fputcsv($fp,$result, ";");
}
flush();
fclose($fp);
Windows needs a BOM to know that a file is UTF-8 and open it correctly.
Before your foreach, add the following line:
fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));