I'm trying to generate and then by default download csv file using php script. I have code which is working fine in chrome and internet explorer but the same script does not works in firefox. In firefox script does not generating csv file properly. Following are the script
require('core.php');
$master = new db();
$s = $master->getRecords();
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
download_send_headers(site_title .' '. date("d M Y") . ".csv");
echo array2csv($s);
die();
HTML and JS
<a class="list-group-item" href="javascript:void(0);" onclick="return exportUser();">Export to CSV</a>
function exportUser(){
$.ajax({
url: 'get_csv.php',
type: 'POST',
success: function() {
window.location = 'get_csv.php';
}
});
}
I also attached a screenshot for understanding.
Try adding this header:
header('Content-Type: application/csv; charset=utf-8');
You need to add an event to your js function
function exportUser(event) will do it
Related
public function exportCsv()
{
$this->download_send_headers("subscriber_list_export_" . date("Y-m-d") . ".csv");
$model = $this->getModel('clientsdatas');
$array = $model->getCsvArray();
echo $this->array2csv($array);
die();
}
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
$row['id'] = '="' . $row['id'] . '"';
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
}
If you think the problem would be PHP or CSV, you probably were not paying attention to the data-source, because one can query the data as required, without having to manipulate an array:
CASE WHEN column IS NULL THEN "-" ELSE column END AS column
I'm trying to fetch and download data using php to csv file, for task i found a good answer, this is working for me on all browser except firefox.
$s = $master->getUser();
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
/*header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename={$filename}");
// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies*/
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
download_send_headers(site_title .' '. date("d M Y") . ".csv");
echo array2csv($s);
die();
In Firefox file extension does not showing in csv, it is looking like application file.
Have you tried removing these in your code:
header("Content-Type: application/force-download");
header("Content-Type: application/download");
Just retain:
header("Content-Type: application/octet-stream");
fputcsv function write data fro 6th row. But I want to write data from first row.
what I have tried
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
what looks like in MS-Excel
what looks like in Notepad
I have a simple web page which pulls some data from DB and displays it in a html table and stores the data in some session variables.
I have a button "Export to csv" which calls a page which exports the results to a csv file.
The code for ExportToCsv file is:
<?php
session_start();
include "conn/sqlserverConn.php";
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
$data = $_SESSION['data'];
$rows = $_SESSION['row'];
$header = $_SESSION['header'];
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
$file = fopen("php://output", 'w');
fputcsv($file,$header);
for($i=0; $i<$rows; ++$i)
{
$valuesArray=array();
foreach($data[$i] as $name => $value)
{
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray);
}
fclose($file);
die();
?>
My code is working flawlessly in firefox but it is not working in chrome or IE. On chrome and IE it is showing error 404 (Object not found!). Please tell me what is the problem ?
As outlined in the blog article at: http://www.exchangecore.com/blog/php-output-array-csv-headers/, I tested the following and it worked in IE8-11, and in chrome version 34. I presume it will work in other browsers fine as well.
Headers to use:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $fileName);
I've created a .php file that write out js code like that:
<?
//my_js.php
// javascript header
header('Content-type: text/javascript');
// Date in the past
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// always modified
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// HTTP/1.1
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
?>
//
// js code here
//
then i include the script above in my index.php file like this:
<script type="text/javascript" src="my_js.php?id=<? echo $id ?>">
</script>
This works perfect in Firefox, but SAFARI and CHROME doesn't include my_js.php file at all!
What i'm doing wrong?
**edit:
this is the rendered html in index.php:
<script type="text/javascript" src="my_js.php?id=new"></script>
and this is the my_js.php code:
(it's a very big file so i write out only the first few lines)
var g = { sitepath: "myNullUrl" }
function getBrowserWidth(){
if (window.innerWidth){
return window.innerWidth;}
else if (document.documentElement && document.documentElement.clientWidth != 0){
return document.documentElement.clientWidth; }
else if (document.body){return document.body.clientWidth;}
return 0;
}
that's a strange problem 'cos while i'm viewing source code from Crome/Safari i can access the js file and it seems to be error free!
I'm using Chrome 6.04 and Safari 5, both for mac.
It may be because it is expecting the file-extension to be my_js.js. If this is the case, save your PHP file as my_js.js, then, assuming you're using Apache, use the Apache Directive: ForceType:
Like so:
<Location /your/path/my_js.js>
ForceType application/x-httpd-php
</Location>
Good luck!
Maybe set Content-Disposition: inline; header?
<?php
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Content-type: text/javascript");
header("Content-Disposition: inline; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
} else {
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
}
header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
}
include("my-file.js
");
?>
It should work. Or you can change this:
header('Content-type: text/javascript');
to this
header('Content-type: application/javascript');
Note:
application/javascript: JavaScript; Defined in RFC 4329 but not accepted in IE 8 or earlier
text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support