I am trying to export some data as csv using the following code but it appends an unwanted 'null' at file end.
$array = array();
$fileName = 'test.csv';
$array[0] = ['Name','Email'];
$array[1] = ['Test','test#email.com'];
$fileObj = new FileStream();
$fileObj->array_to_csv_download($array,$fileName);
array_to_csv_download code -
$f = fopen('php://memory', 'w');
foreach ($array as $line) {
$this -> logObj -> LogError(" CSV ".json_encode($line));
fputcsv($f, $line, $delimiter);
}
fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
fpassthru($f);
Output -
Name,Email
Test,test#email.com
null
Am I missing something?
How about generating CSV directly? Works for me.
header('Content-Type: application/csv');
foreach($array as $fields) {
echo implode(",", $fields)."\n";
}
Related
I am trying to output an array to CSV with PHP using the below code. It is, however outputting the entire HTML page to the CSV. How can I write this so it only outputs the array to the CSV and not the code of the page?
I believe the problem is to do with:
$f = fopen('php://output', 'w');
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
// open raw memory as file so no temp files needed, you might run out of memory though
$f = fopen('php://output', 'w');
// loop over the input array
foreach ($array as $line) {
// generate csv lines from the inner arrays
fputcsv($f, $line, $delimiter);
}
// reset the file pointer to the start of the file
fseek($f, 0);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="'.$filename.'";');
// make php send the generated csv lines to the browser
fpassthru($f);
}
array_to_csv_download($data_array);
You are sending output before your headers.
You can simplify to:
<?php
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$f = fopen('php://output', 'w');
foreach ($array as $line)
fputcsv($f, $line, $delimiter);
}
Make sure you do not output anything else prior to those header calls.
Comparing two multidimensional array and get the result by matching values. How to create and download the csv file for those respective values. I have tried this code below
$mycsvfile_result = array();
foreach ($mycsvfile as $arr1)
{
foreach ($mycsvfile_log as $arr2)
{
if($arr2[2] == $arr1[0])
{
$mycsvfile_result[] = array($arr2[2], $arr2[7]);
}
}
}
tried code 1 for fputcsv
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="demo.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$fp = fopen('php://output', 'w');
$data=$mycsvfile_result;
foreach ( $data as $line )
{
fputcsv($fp, $line);
}
fclose($fp);
exit();
tried code 2 for fputcsv
$csv = "col1,col2 \n";//Column headers
foreach ($mycsvfile_result as $record)
{
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('demo.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to demo.csv';
The above code 1 results in
id1,email1 id2,email2 id3,email3
The above code 2 results in
col1,col2 id1,email1 id2,email2 id3,email3 Data saved to demo.csv
I'm working in linux machine, I forget to give the folder permission to upload the csv file. After giving folder permission it works like a charm :)
I'm creating a CSV from an array. The problem is that I dont want to download it but I want to save it to a folder in my server. This is my function
protected function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachement; filename="'.$filename.'";');
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}
How can I make it so that it saves it to the server? Thanks!
Remove headers.
Use you server's file path in fopen handle.
$f = fopen('path of file on server ', 'w');
I'm trying to export my data into a csv file and let the user to download it. I'm using fputcsv() function, but in the file, the data are written in a single cell instead of adjacent cells. I don't know what is the problem. Please help me. here is my code
session_start();
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename=report_'.time().'.csv;');
$data = $_SESSION['data'];
$file = fopen('php://output','w');
foreach($data as $i=>$value)
{
fputcsv($file, $value,";");
}
fclose($file);
and this is how the file looks like..
try like
$data = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"');
$fp = fopen('php://output', 'w+');
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="data.csv"');
foreach($data as $line){
$val = explode(",",$line);
fputcsv($fp, $val);
}
fclose($fp);
or try may be according your data
foreach($data as $i=>$value)
{
fputcsv($file, $value);
}
Use , instead of ; for delimiter
Replace fputcsv($file, $value,";"); with fputcsv($file, $value, ","); and try
Example:
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename=report_'.time().'.csv;');
$data = array(array('one' => 1, 'two' => 2));
$file = fopen('php://output','w');
foreach($data as $i => $value)
{
//fputcsv($file, $value, ";");
fputcsv($file, $value, ",");
}
fclose($file);
I have to create and write into a csv file. Now I tried with the following code:
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="data.csv"');
$data = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"');
$fp = fopen('php://output', 'w+');
foreach($data as $line){
$val = explode(",",$line);
fputcsv($fp, $val);
}
fclose($fp);
It gives me my source file code also in the csv file? How can I solve this?
Wrap it in <?php and ?> tags.
Change the first header to header('Content-type: text/csv');
header() needs to come after the array and fopen
try this:
$data = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"');
$fp = fopen('php://output', 'w+');
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="data.csv"');
foreach($data as $line){
$val = explode(",",$line);
fputcsv($fp, $val);
}
fclose($fp);
Try this:
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$data = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"');
$fp = fopen('php://output','w');
foreach ($data as $line) {
fputcsv($fp, array_map('utf8_decode',array_values($line)), ',', '"');
}
fclose($fp);