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');
Related
I need to create csv and then send it to mail as attachment. I am using headers which is getting me downloaded the csv file. and if I remove the below then no CSV is created.
header('Content-Type: text/csv');
I dont want to download the file instead save it in some temp folder and then send it as attachment in mail.
below is what i did..
$this->load->library('excel');
$filename = tempnam(sys_get_temp_dir(), 'export') . '.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$filename.'"');
//$f = fopen('php://output', 'w');
$f = fopen($filename, 'w');
$csv_header = array("name","number","address");
fputcsv($f, $csv_header);
ob_flush();
flush();
foreach ($data as $value) {
$data_array = array($value['name'],$value['number'],$value['address']);
fputcsv($f, $data_array);
}
}
fclose($f);
Solution
Remove these header lines as they are used to download files to a browser.
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$filename.'"');
On the serverside, you should be good with just
$this->load->library('excel');
$temporary_filename = tempnam(sys_get_temp_dir(), 'export') . '.csv';
$file_write_stream = fopen($temporary_filename, 'w');
$csv_header = array("name","number","address");
fputcsv($file_write_stream, $csv_header);
foreach ($data as $value) {
fputcsv($file_write_stream, array($value['name'], $value['number'], $value['address']));
}
fclose($file_write_stream);
Pro tips:
Use meaningful variable names
For sending the email, I'd recommend using the PHP mailer library. It makes sending email super simple and you can add an attachment with just one line. In your case:
$mail->addAttachment($temporary_filename);
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.
So i have two files one reads an exsisting CSV edits and changes some values and then writes a new file. My goal is it that the new csv is then directly downloadable. It works in two seperate files but i have to combine them... it does not work.
ยด
If i have the two seperate it does work but when i combined them like this it did not work:
$file_name = 'sample.csv';
$fp = fopen($file_name, 'w');
foreach ($infos as $info) {
fputcsv($fp,explode(';',$info));
}
fclose($fp);
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=sample.csv');
readfile('sample.csv');
//FIRST FILE (JUST THE LAST BIT WHERE THE FILE IS CREATED)
$file_name = 'sample.csv';
$fp = fopen($file_name, 'w');
foreach ($infos as $info) {
fputcsv($fp,explode(';',$info));
}
fclose($fp);
echo "<script>window.open('.../Leon/downloadFile.php')</script>";
?>
//SECOND FILE AT (.../Leon/downloadFile.php)
<?php
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=sample.csv');
readfile('sample.csv');
?>
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";
}
I have an array of values that I want to put into a csv file and then download it:
$f = fopen("./tokens.csv", "w");
foreach ($tokens as $line) {
fputcsv($f, array("1"=>$line));
}
fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="tokens.csv"');
fpassthru($f);
The result is that the file on the server is populated with the contents of the token array, but the downloaded file is an empty csv file.
You've opened the file in "write-only" mode. You didn't tell PHP that you wanted to read it. You need to open the file in "read-write" mode.
$f = fopen("./tokens.csv", "w+");
DOCS: http://www.php.net/manual/en/function.fopen.php
P.S. It's text/csv not application/csv. Also you misspelled attachment.
NOTE: "w+" will overwrite the file each time. Do you want to store the file on the server, or is this just a download script? If you are not storing the file and you are just using this to download a file, you can just write to the output stream directly:
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="tokens.csv"');
$f = fopen("php://output", "w");
foreach ($tokens as $line) {
fputcsv($f, array("1"=>$line));
}
This will work:
$f = fopen("./tokens.csv", "w+");
foreach ($tokens as $line) {
fputcsv($f, array("1"=>$line));
}
fseek($f, 0);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="tokens.csv"');
fpassthru($f);
Attachment and NOT attachement
The correct Mime type for CSV files is:
header('Content-Type: text/csv');