csv file is not downloading within for loop - php

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 :)

Related

PHP array to CSV outputting entire html page

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.

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

Use fputcsv create a .csv file and also update the page with table data

I am using fputcsv to create a .csv file and download it automatically for the user. This works great:
function create_csv($data, $not_valid, $csv_old_count, $file_name) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
fputcsv($fp, $line);
}
fclose($fp);
exit();
}
But, I would also like to update the page with a table that shows the data that was put into the csv file. When trying to do this by removing exit() and adding a loop to write the table, that data also get written to the csv file instead of writing to my page.
How can I create and download the .csv and also write some HTML to the page?
You can add the output to your foreach() -
foreach ( $data as $line ) {
fputcsv($fp, $line);
$newLines .= $line . "<br />"; // you'll have to figure out formatting
}
fclose($fp);
echo $newLines; // place this where you want to display the lines

downloading a .csv file in PHP stops my other PHP code

With PHP I'm opening a .csv file, I'm adding som rows in it then I'm downloading the new .csv file.
the code I'm using is:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen("csv.csv","r");
$list = array();
while(! feof($file))
{
$list[] = (fgetcsv($file));
}
fclose($file);
$list[] = array('name5', 'town5');
$list[] = array('name6', 'town6');
$list = array_filter($list);
outputCSV($list);
function outputCSV($list) {
$output = fopen("php://output", "w");
foreach ($list as $row) {
fputcsv($output, $row);
}
fclose($output);
}
My issue is the other PHP code in the page don't generate, and only the .csv file is being download.
for exemple if I'm adding:
echo "test";
it won't be display and only csv.csv will be downloaded
How can I display my "echo test;" ?
With the headers you are telling the browser to download a CSV, there is no where to output your echo.
You could make a page with the information you want to show that has an iframe with the csv code in it.

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