fputcsv displaying data in browser instead of downloading data as CSV file - php

I am using fputcsv function to export data as CSV file but it is displaying all data in browser instead of downloading this as CSV file. Here is my code
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export.csv');
$output = fopen('php://output', 'w');
fputcsv($output, $column_headings);
fclose($output);
Data is displaying correctly in browser but I want to download this as export.csv file.I do not know what is wrong with this. Any suggestion regarding this will be appreciated.

You cannot control how the browser handles the text/csv content type, however, you can tell the browser it should treat the data as binary.
header('Content-Type: application/octet-stream');

I added ob_start(); at the start of my file and now I can export files.

Related

PHP Output MS SQL to a downloadable CSV or Excel file

I'm trying to output data returned by an MS SQL query to an Excel or CSV file with PHP.
I've used the script in this answer and can output the file OK. Without the header lines (at the bottom of my code) it saves in my server's folder structure rather than outputs as a download to the browser.
If I add the header lines, it ouputs to a CSV file but writes the page's HTML to the file rather than the extract from the database! Am I missing a setting somewhere? I tried running the code on a page with no HTML in it (PHP and SQL code only), but it still happens.
// Give the file a suitable name:
$FileName= $PartNumber.".csv";
$fp = fopen($FileName, 'w');
// Connect to MS SQL server; the actual database is chosen in the form
// ConnSQL defined in inc/dbconn/config.php
ConnSQL($idDatabase);
// the query is a biggie; here it is:
require 'inc_sql.php';
// run it through the SQL server
$rstBOM = sqlsrv_query($GLOBALS['ConnSQL'], $sqlBOM);
while ($export= sqlsrv_fetch_array($rstBOM, SQLSRV_FETCH_ASSOC)) {
if (!isset($headings))
{
$headings = array_keys($export);
fputcsv($fp, $headings, ',', '"');
}
fputcsv($fp, $export, ',', '"');
}
// force download csv - exports HTML to CSV!
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="'.$FileName.'"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ". filesize($FileName));
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="'.$FileName.'"');
fclose($fp);
Any ideas where I'm going wrong please?
You need to output your csv file to the browser simply by putting
readfile($FileName);
At the end of your code after the fclose($fp); function.
Otherwise, browser receives the headers for files, but no content in sent from your PHP code.
You could also generate your csv file on the fly and just echo $csvFileContents; instead. This would prevent server from creating and writing data to file, which could lead to security breaches.
Good luck!

Generate a CSV file from XML stream for MYSQL Import

I am trying to get data from a online XML file and save in CSV format. I was able to save the CSV file but the data is not saved properly as a comma separated CSV and due to which the data has no delimiter and is not getting imported in mysql properly. It imports in mysql in one column itself where there are 8 columns and it should be imported in different columns as per CSV data.
I am using
$report = stream_get_contents($request->getReport());
// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('data.csv','w+');
fwrite($file,$report);
fclose($file);
You are writing the lines without formatting them properly first. You should use fputcsv instead of fwrite.

How do I generate a pdf-file from a binary file

The binary data is simply the actual file, or rather the important contents of that file, just without file name.
$base64 = /* some base64 encoded data fetched from somewhere */;
$binary = base64_decode($base64);
And there you have the file data/contents of the file in the $binary variable. From here, it depends on what you want to do. You can write the data to a file, and you get an "actual" PDF file:
file_put_contents('my.pdf', $binary);
You can spit the data out to the browser with an appropriate header, and the user will receive something that looks like a PDF file to him:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
it generates a pdf file but it not opening..it show error occur.
//below is the php file
<?php
$base64 = "/here binary code/";
$binary = base64_decode($base64);
file_put_contents('my.pdf',$binary);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
?>
then how to compare downloaded file with original file.
I think your problem is that you don't have proper data in $base64 variable. You're simply making PDF response, but content of that response is not PDF file.
(You can try to read content from some real PDF, fill this data to $binary, and it should work.)

PHP download CSV

Stuck on what is likely a silly problem and only posting after reading several related threads.
Have a page with a lot going on, one of the form options I'm trying to add is so the user can select to download array results in CSV. Problem is HTML header info is coming through in addition to the CSV data I want.
Code is:
function Array2Csv($result, $filename){
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=' .$filename);
$output = fopen('php://output', 'w');
while($row = mysql_fetch_assoc($result)) {
fputcsv($output, $row,'|','"');
}
}
Problem is the result file includes BOTH undesired markup (headers and scripting references) in addition to the CSV itself. Desired output should only include the CSV data.
You have send the the header before any output was send. Disable view and layout.
See also http://php.net/manual/en/function.header.php

Creating CSV file from PHP, no line feed

I am trying to create a CSV file from my web page and am having issue with line feeds/carraige returns.
I am building up the CSV string via Javascript and then adding '\u000A' + "\u000D" to the end of each line.
I then pass my CSV sting to a PHP program via AJAX. My PHP logic to create the file is: -
header('Content-Disposition: attachment; filename="effortTool.csv"');
header('Content-Type: text/plain');
header('Content-Length: ' . strlen($content));
When I view the file it does not contain any line feeds/carraige retuns.
If I output the CSV string as an "alert", I can see it is formatting corerctly. This tells me that the line feeds characters are being added but are being lost when downloaded as a file.
Any ideas?
Thanks
Martin
Use following code to generate CSV file:-
ob_clean();
header("Content-type: application/vnd.ms-excel");
header( "Content-disposition: filename=effortTool.csv");
header('Content-Length: ' . strlen($content));
print $content;
exit;
This was working for me, hope it'll work for you too.
You put the file content on the meta data of type Content-Length, this is wrong.
Try this: php download file: header()

Categories