Skip to content

Instantly share code, notes, and snippets.

@technoknol
Last active August 29, 2015 14:01
Show Gist options
  • Save technoknol/34319c60627777047768 to your computer and use it in GitHub Desktop.
Save technoknol/34319c60627777047768 to your computer and use it in GitHub Desktop.
Add new rows to existing CSV without editing it and Download new one.
<?php
/*
* Author : TechnoKnol
* Blog : http://technoknol.blogspot.com
*
*/
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);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment