How to make fputcsv "echo" the data - php

I need a way to make the fputscv function write data to the browser on-the-fly instead of creating a temporary file, saving data into that file and doing a echo file_get_contents().

Found this on the PHP docs website, first comment under the function reference:
function outputCSV($data) {
$outstream = fopen("php://output", 'w');
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals, ';', '"');
}
array_walk($data, '__outputCSV', $outstream);
fclose($outstream);
}
And a second option:
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
fputcsv($csv, array('blah','blah'));
rewind($csv);
// put it all in a variable
$output = stream_get_contents($csv);
Hope this helps!
BTW the PHP docs should always be your first stop when trying to figure things out. :-)

By a comment on the PHP site
<?php
$out = fopen('php://output', 'w');
fputcsv($out, array('this','is some', 'csv "stuff", you know.'));
fclose($out);
?>

As the original asker wanted to "write to the browser on the fly", maybe is worth noting (as was my case and noone mentioned it) that if you want to force a file name and a dialog asking to download a file in the browser, you must set the proper headers before outputting anything with fputcsv:
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=myFile.csv');

Producing a CSV is actually not all that difficult (parsing a CSV is a little bit more involved).
Sample code for writing a 2D Array as CSV:
$array = [
[1,2,3],
[4,5,6],
[7,8,9]
];
// If this CSV is a HTTP response you will need to set the right content type
header("Content-Type: text/csv");
// If you need to force download or set a filename (you can also do this with
// the download attribute in HTML5 instead)
header('Content-Disposition: attachment; filename="example.csv"')
// Column heading row, if required.
echo "Column heading 1,Column heading 2,Column heading 3\n";
foreach ($array as $row) {
$row = array_map(function($cell) {
// Cells containing a quote, a comma or a new line will need to be
// contained in double quotes.
if (preg_match('/["\n,]/', $cell)) {
// double quotes within cells need to be escaped.
return '"' . preg_replace('/"/', '""', $cell) . '"';
}
return $cell;
}, $row);
echo implode(',', $row) . "\n";
}

Related

Export HTML table to CSV file

I am making a script which gets a table from your mail and puts it into a CSV file.
This is the code I use to transfer my html table to CSV
$html = str_get_html($outputstr);
// For Excel
header('Content-type: application/ms-excel');
// Download File
header('Content-Disposition: attachment; filename=sample.csv');
$fp = fopen("php://output", "w");
// Take out empty lines
foreach($html->find('tr') as $element) {
$td = array();
foreach( $element->find('th') as $row) {
$td [] = $row->plaintext;
}
foreach( $element->find('td') as $row) {
$td [] = $row->plaintext;
}
fputcsv($fp, $td);
}
fclose($fp);
The only problem that I'm getting is that when I am opening the CSV file, some of the empty columns have a strange character:
I cannot read through with my PHP script to export it to a database
fgetcsv($handle, 1000, "\t");
How can I fix this problem?
Do I fix this by modifying the code on the part where I create the CSV file or where I read the CSV file when I'm transferring it to a MySQL database?
When I use an online html to CSV converter it works fine and I am not facing this issue then.
If there is any code needed then I'd love to share it.
Any help would be appreciated.
Have you tried setting your charset to UTF-8? Additionally, you're not setting this up as a CSV with your header, instead it is an Excel file.
header("content-type:application/csv;charset=UTF-8");

how to convert php file into csv

$lang['add_a']="Add a";
$lang['ban_zon_link']="Banner, Zone linking";
$lang['ban_zon_link_help']="Zones linking with Banners";
$lang['manage_ban_zon_link_help']="Manage banner zone linking -";
$lang['adtag']="Ad Tag";
I saved above lines into php file as lang.php need to convert php file into csv in code level.
You could open the file as csv, iterate over your array and use fputcsv
foreach($lang as $myrow) {
fputcsv($output, $myrow);
}
http://php.net/manual/en/function.fputcsv.php
You can use fputcsv:
<?php
$lang = ...
$fp = fopen('file.csv', 'w');
// Insert array keys as CSV header
fputcsv($fp, array_keys($lang);
// Insert values as first data row
fputcsv($fp, array_values($fields));
fclose($fp);
?>
This works if you have only row record in your $lang array as your example suggests. This will create a 2 rows CSV with an header (the keys of the $lang array) and one data row (the values of the array.
If you are aiming at a different thing please clarify your question.
include("lang.php");
foreach($lang as $l)
{
file_put_contents("csv_file.csv",$l.",",FILE_APPEND);
// csv_file.csv is the name of your file
}
file_put_contents("csv_file.csv","\n",FILE_APPEND);
try with this:-
/* $lang['add_a']="Add a";
$lang['ban_zon_link']="Banner, Zone linking";
$lang['ban_zon_link_help']="Zones linking with Banners";
$lang['manage_ban_zon_link_help']="Manage banner zone linking -";
$lang['adtag']="Ad Tag";*/
if above php code in lang.php:-
require_once "lang.php";
$fp = fopen('file.csv', 'w');
fputcsv($fp, array_keys($lang));
fputcsv($fp, array_values($lang));
fclose($fp);
To read in a PHP script and write it out, I've done the following...
<?php
$file="part1.php";
$outFile="part1.csv";
$in = file($file);
$out = fopen($outFile, "w");
foreach($in as $line ) {
$parts = explode("=", $line);
if ( count($parts) == 2 ) {
fputcsv($out, array($parts[0],"=",rtrim($parts[1],";".PHP_EOL)));
}
}
fclose($out);
which for your example ( as part1.php) gives...
$lang['add_a'],=,"""Add a"""
$lang['ban_zon_link'],=,"""Banner, Zone linking"""
$lang['ban_zon_link_help'],=,"""Zones linking with Banners"""
$lang['manage_ban_zon_link_help'],=,"""Manage banner zone linking -"""
$lang['adtag'],=,"""Ad Tag"""
$file = 'file.csv';
header( "Content-Type: text/csv;charset=utf-8" );
header( "Content-Disposition: attachment;filename=\"$file\"" );
header("Pragma: no-cache");
header("Expires: 0");
$fp= fopen('php://output', 'w');
foreach ($lang as $fields)
{
fputcsv($fp, $fields);
}
fclose($fp);
exit();

Array to CSV file for vertical layout and failed to put the header row

I have a problem to create a horizontal layout for the csv
here is an array which I have convert it to json-format
{"datetime":["2000-01-01","2000-01-02","2000-01-03","2000-01-04","2000-01-05","2000-01-06","2000-01-07","2000-01-08","2000-01-09","2000-01-10","2000-01-11","2000-01-12","2000-01-13","2000-01-14","2000-01-15","2000-01-16","2000-01-17","2000-01-18","2000-01-19","2000-01-20","2000-01-21","2000-01-22","2000-01-23","2000-01-24","2000-01-25","2000-01-26","2000-01-27","2000-01-28","2000-01-29","2000-01-30","2000-01-31","2000-02-01","2000-02-02","2000-02-03","2000-02-04","2000-02-05","2000-02-06","2000-02-07","2000-02-08","2000-02-09","2000-02-10","2000-02-11","2000-02-12","2000-02-13","2000-02-14","2000-02-15","2000-02-16","2000-02-17","2000-02-18","2000-02-19","2000-02-20","2000-02-21","2000-02-22","2000-02-24","2000-02-25","2000-02-26","2000-02-27","2000-02-28","2000-02-29","2000-03-01","2000-03-02","2000-03-03","2000-03-04","2000-03-05","2000-03-06","2000-03-07","2000-03-08","2000-03-09","2000-03-10","2000-03-11","2000-03-12","2000-03-13","2000-03-14","2000-03-15","2000-03-16","2000-03-17","2000-03-18","2000-03-19","2000-03-22","2000-03-23","2000-03-24","2000-03-28","2000-03-29","2000-03-30","2000-03-31","2000-04-01","2000-04-02","2000-04-03","2000-04-04","2000-04-05","2000-04-06","2000-04-07","2000-04-08","2000-04-09","2000-04-10","2000-04-11","2000-04-12","2000-04-13","2000-04-14","2000-04-15","2000-04-16","2000-04-17","2000-04-18","2000-04-19","2000-04-20","2000-04-21","2000-04-22","2000-04-23","2000-04-24","2000-04-25","2000-04-26","2000-04-27","2000-04-28","2000-04-29","2000-04-30","2000-05-01","2000-05-02","2000-05-03","2000-05-04","2000-05-05","2000-05-06","2000-05-07","2000-05-08","2000-05-09","2000-05-10","2000-05-11","2000-05-12","2000-05-13","2000-05-14","2000-05-15","2000-05-16","2000-05-17","2000-05-18","2000-05-19","2000-05-20","2000-05-21","2000-05-22","2000-05-23","2000-05-24","2000-05-25","2000-05-26","2000-05-27","2000-05-28","2000-05-29","2000-05-30","2000-05-31","2000-06-01","2000-06-02","2000-06-03","2000-06-04","2000-06-05","2000-06-06","2000-06-07","2000-06-08","2000-06-09","2000-06-10","2000-06-11","2000-06-12","2000-06-13","2000-06-14","2000-06-15","2000-06-16","2000-06-17","2000-06-18","2000-06-19","2000-06-20","2000-06-21","2000-06-22","2000-06-23","2000-06-24","2000-06-25","2000-06-26","2000-06-27","2000-06-28","2000-06-29","2000-06-30","2000-07-01","2000-07-02","2000-07-03","2000-07-04","2000-07-05","2000-07-06","2000-07-07","2000-07-08","2000-07-09","2000-07-10","2000-07-11","2000-07-12","2000-07-13","2000-07-14","2000-07-15","2000-07-16","2000-07-17","2000-07-18","2000-07-19","2000-07-20"],"Reservoir Level":["102.95","103.01","103.05","102.93","102.78","102.83","102.78","102.87","102.92","102.97","103.02","103.05","103.29","103.3","103.29","103.15","103","102.92","102.95","102.98","102.9","102.8","102.8","102.81","102.86","102.9","102.92","102.94","102.96","102.98","102.99","102.99","102.3","103","103.02","103.03","103.04","103.05","103.05","103.06","103.1","103.15","103.19","103.19","103.2","103.3","103.32","103.33","103.34","103.19","102.98","102.89","102.89","102.91","102.96","103.05","103.06","103.07","103.27","103.31","103.28","103.12","102.95","102.94","102.97","102.98","103.02","103.02","103.08","103.14","103.18","103.54","103.57","103.48","103.35","103.21","103.12","103.2","103.3","103.38","103.4","103.4","103.4","103.4","103.4","103.34","103.34","103.27","103.27","103.28","103.28","103.3","103.3","103.3","103.3","103.25","103.2","103.2","103.14","103.14","103.1","103.1","103","103","103.25","103.25","103.35","103.35","103.45","103.45","103.5","103.5","103.46","103.46","103.37","103.37","103.26","103.26","103.22","103.22","103.26","103.31","103.31","103.49","103.49","103.34","103.34","103.19","103.19","103.21","103.21","103.27","103.27","103.33","103.37","103.37","103.42","103.42","103.33","103.33","103.26","103.26","103.19","103.19","103.35","103.35","103.48","103.48","103.55","103.55","103.55","103.6","103.46","103.46","103.46","103.33","103.33","103.33","103.62","103.62","103.62","103.62","103.6","103.5","103.5","103.38","103.38","103.24","103.24","103.26","103.26","103.3","103.3","103.2","103.2","103.16","103.23","103.23","103.26","103.26","103.29","103.29","103.31","103.31","103.32","103.32","103.35","103.35","103.37","103.42","103.42","103.3","103.3","103.25","103.25","103.28","103.28","103.3","103.3","103.31","103.31","103.25","103.25","103.18","103.18","103.18","103.18","103.22","103.12","103.13","103.14","103.16","103.23","103.65","103.52","104.03","103.84","103.68","103.59","103.45","103.31","103.2","103.11","103.13","103.15","103.19","103.22","103.25","103.27","103.29","103.3","103.32","103.24","103.13","103.12","103.17","103.21","103.25","103.15","103.04","103.07","103.09","103.1","103.11","103.11","103.11","103.1","103.05","103.08","103.1","103.11","103.11","103.11","103.1","103.1","103.11","103.13"],"Rainfall":[null,"14.5","4.5","7","2.5","54",null,"18","2",null,"1",null,"24.5","9",null,null,null,null,null,"0.5",null,null,null,null,"10.5",null,null,null,null,null,null,null,null,null,null,null,"5",null,null,null,"17.5","7.5",null,null,"7","20",null,null,null,null,null,null,null,"5","10","9.5","0","0","45","0","42","0","0","0","0","2.5","27.5","0","29","39","2","93","6","0","0","14","0","8.5","0","43.5","31.5","0","0","32","32","0","0","2","2","14","14","20","20","4","4","0","0","5.5","5.5","0","0","17.5","17.5","0","0","16","16","0","0","14","14","0","0","0.5","0.5","0","0","0","0","0","0","8","8","3.5","3.5","69.5","69.5","0","0","0","0","39.5","39.5","5","5","9","9","1","1","0","0","6.5","6.5","11.5","11.5","14.5","14.5","0","0","0","0","0","0","0","21.5","21.5","21.5","0","0","0","0","0","0","54.5","54.5","0","0","14.5","14.5","12","12","0","0","0","0","0","0","0","0","0","0","0","0","31.5","31.5","0","0","0","0","0","0","0","0","0","0","0","0","24.5","24.5","0","0","18","18","0","0","12.5","12.5","6.5","6.5","1","1","0","0","0","0","0","0.5","0","0","12.5","1","57.5","0","63","0","0","40","5.5","0","0","0","0","0","0.5","0.5","0","0","0","0","12.5","2.5","0.5","1","27","11","3.5","13","0","4.5","0","0","0","0","0","0","0","0","0","1.5","0","0","0","0","2","9.5"]}
and here I have a develop a simple function to output it as csv
function convert_to_csv($input_array, $output_file_name, $delimiter) {
$temp_memory = fopen('php://memory', 'w');
foreach ($input_array as $key => $set) {
fputcsv($temp_memory, $set, $delimiter, '"');
}
fseek($temp_memory, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
fpassthru($temp_memory);
}
here the layout that i don't want it to be
and this is the layout that I want it to be
as you can see I failed to find a way to put the header row. Appreciate your helps. Thanks
You need transposition.
foreach($input_array as $key=>$set){
foreach($set as $row=>$val){
$lines[$row][$key] = $val;
}
}
For input_array like:
{"datetime":["2000-01-01","2001-01-01"], "Resovior":[1,2], "RainFall":[2,2]}
will produce $lines like
$lines[0]: {"datetime": "2000-01-01", "Resovior": 1, "RainFall": 2}
$lines[1]: {"datetime": "2001-01-01", "Resovior": 2, "RainFall": 2}
Keep in mind that every element of $lines refers to one row in your file, and its' elements are the columns.
Then you can put rows like this:
foreach ($lines as $line)
{
fputcsv($temp_memory,$line,$delimeter,"'");
}
The way to add a header is the same.
$header = array("DateTime", "Revisior", "Rainfall");
fputcsv($temp_memory, $header, $delimeter, "'");
Your output file is wrong formatted, must be like that:
DateTime;Reservoir;Rainfall;
date1;res1;rain1;
date2;res2;rain2;
date3;res3;rain3;
but your current file looks like this one:
date1;date2;date3;
res1;res2;res3;
rain1;rain2;rain3;
You must iterate trough array recursively in order to set the correct format.
A possible solution (taken from https://stackoverflow.com/a/6600130/3518053 ) :
$fh = fopen('file.csv', 'w');
// write out the headers
fputcsv($fh, array_keys($data));
// write out the data
for ($i = 0; $i < count($data['dates']); $i++) {
$temp = array($data['dates'][$i], $data['type1'][$i], $data['type2'][$i]);
fputcsv($fh, $temp);
}
Regards

Export database to CSV with columns PHP

Here my php script to export database info to CSV file.
I dont arrive to put any structure to correctly tidy my infos in my CSV file.
For example, put all names in a name column, all emails in an email column... etc
include_once('conf.php');
include_once('BDD.php');
header('charset=UTF-8');
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$bdd = new BDD($conf['bddhost'], $conf['bddport'], $conf['bddname'], $conf['bdduser'], $conf['bddpass']);
$sql = "SELECT * FROM user";
$qry = $bdd->prepare($sql);
// Execute the statement
$qry->execute();
$data = fopen('/tmp/db_user_export_".time().".csv', 'w');
while ($row = $qry->fetch(PDO::FETCH_ASSOC))
{
// Export every row to a file
fputcsv($data, $row);
echo ''.$row['prenom'].' '
.$row['nom'].' '
.$row['email'].' '
.$row['cp'].' '
.$row['information'].'
';
}
fclose($data);
You don't want to use echo as you are creating the file with fputcsv
while ($row = $qry->fetch(PDO::FETCH_ASSOC))
{
// Export every row to a file
fputcsv($data, $row);
}
// reset the file pointer to the beginning of the file
rewind($data);
// dump the csv file and stop the script
fpassthru($data);
exit;
Syntax errors:
$data = fopen('/tmp/db_user_export_".time().".csv', 'w');
^-- ^-- ^-- ^---
You're mixing string quoting styles, so your filename is literally going to contain the characters ", ., t, etc... in it.
Try
$data = fopen('/tmp/db_user_export_' .time() .'.csv', 'w');
^----------^---
instead. Note the change from " -> '.
Since your result is an array, this may help you out:
Convert php array to csv string
if(!function_exists('str_putcsv'))
{
function str_putcsv($input, $delimiter = ',', $enclosure = '"')
{
// Open a memory "file" for read/write...
$fp = fopen('php://temp', 'r+');
// ... write the $input array to the "file" using fputcsv()...
fputcsv($fp, $input, $delimiter, $enclosure);
// ... rewind the "file" so we can read what we just wrote...
rewind($fp);
// ... read the entire line into a variable...
$data = fread($fp, 1048576);
// ... close the "file"...
fclose($fp);
// ... and return the $data to the caller, with the trailing newline from fgets() removed.
return rtrim($data, "\n");
}
}
$csvString = '';
foreach ($list as $fields) {
$csvString .= str_putcsv($fp, $fields);
}
More about this on GitHub, a function created by #johanmeiring.

fputcsv Inserting HTML code into a csv file

I have problem with writing csv file using fputcsv. Its putting the page html also into the csv file. Whats wrong with my code ?
//Excel header
header("Content-Disposition: attachment; filename=\"Delivery_Reports.csv\";" );
header("Content-type: application/vnd.ms-excel");
$out = fopen("php://output", 'w');
$flag = false;
// $result = mysql_query("SELECT * FROM senderids ") or die('Query failed!');
//$sel="SELECT number as MobileNumber ,snum as Sender , msg as Subject ,crdate as Date ,status FROM savemsg WHERE userID='".$_SESSION['id']."' ".$str." ORDER BY sn DESC ";
$result = mysql_query("SELECT `count`, `dnd`, `credit`, `sender_id`, `to`, `message`, `status` FROM `reports` WHERE `unq_id` = '$dlr_id'");
while(false !== ($row = mysql_fetch_assoc($result))){
if(!$flag){
$list = array(
"Total"=>"Total",
"DND"=>"DND",
"Credits"=>"Credits",
"From"=>"From",
"To"=>"To",
"Message"=>"Message",
"Status"=>"Status"
);
// display field/column names as first row
fputcsv($out, array_keys($list), ',', '"');
$flag = true;
}
// array_walk($row, 'cleanData');
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
You can't guarantee, from within a snippet of code, that nothing else will be output. If the code before this snippet is using output buffering, you can discard the HTML using ob_end_clean. If the code after this snippet is causing the problem, you can simply call die to keep it from running at all. However, if the code before this snippet is outputting HTML directly to the browser, or the code after it outputs HTML and absolutely has to run, then you'll have to modify that code in order to solve your problem.
As Tim mentioned, print, echo and outputting to the pseudo-file php://output do exactly the same thing.
You can also use the keyword continue just before you close the file (fclose($f);). This also works lovely.
You can also use exit; after fclose($out); which stopped the output from scraping my html.
I know it's an old question but it gets found in Google so adding this.
If the HTML is being output by the CMS such as WordPress etc. before you try to create the file, it might help to add ob_clean(); and ob_start(); before you output the header.
For example:
function create_csv($records, $columns){
ob_clean();
ob_start();
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="Export.csv"');
$fp = fopen('php://output', 'w+');
// Generate the file content.
fclose($fp);
die();
}

Categories