Wordpress export csv to local /wp-content/uploads - php

I want to put csv file under /wp-content/uploads instead of downloading it . Following php code download this csv file
I am using this php code
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=data.csv');
$stream = fopen('php://output', 'w+');
fputcsv($stream, $csv_header);
foreach($arrCSV as $key => $assoc_row){
$numeric_row = array();
foreach ($csv_header as $header_name) {
mb_convert_variables('UTF-8', $assoc_row[$header_name]);
$numeric_row[] = $assoc_row[$header_name];
}
fputcsv($stream, $numeric_row);

Related

Prevent CSV file downloading (CodeIgnitor)

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 have to files 1:Read and Write CSV & 2: Dowload CSV. Combining and downlaoding doesnt work

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');
?>

Array to CSV to file

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');

File on the server is populated, but is downloaded empty

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');

php://output results in empty file

I have a Wordpress theme template file in use by a page. The template queries the db for an array and then attempts to output the result to a csv file. No output to the browser is expected. Here is code:
/***
* Output an array to a CSV file
*/
function download_csv_results($results, $name = NULL)
{
if( ! $name)
{
$name = md5(uniqid() . microtime(TRUE) . mt_rand()). '.csv';
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "w");
foreach($results as $result)
{
fputcsv($outstream, $result);
}
fclose($outstream);
}
The file is written to the user's downloads directory as expected, but it is empty. I've debugged to verify there is a result of 117 elements. The loop above is executing. It's as though the output buffer is not being flushed or is being cleared by Wordpress.
Could you try to use :
$outstream = fopen("php://output", "a");
instead of :
$outstream = fopen("php://output", "w");

Categories