Jump to content

Trying to start a csv file download using headers in php


rxbanditboy1112

Recommended Posts

The download doesn't start and nothing appears on the screen. This happens in IE and Firefox.

 

header("Content-type: text/plain");
header('Content-Disposition: inline; filename=$file_name.csv');
echo $data; 

 

The data and file name is created before. I have tried a number a bunch of different things with the header, but it is not working. I don't get it....

 

I also tried this:

header('Pragma: no-cache');
header('Expires: 0');
header('Content-Type: text/x-csv');
header('Content-Disposition: attachment; filename=$file_name.csv');
header("Content-Transfer-Encoding: binary");
echo $data; 

this has worked for me

 

<?php
$filename = 'xyz.csv';

header('Content-type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

echo "A, 1, 2, 3\n";
echo "B, 4, 5, 6\n"; 

I tried:

<?php
$filename = 'xyz.csv';

header('Content-type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

echo "A, 1, 2, 3\n";
echo "B, 4, 5, 6\n"; 

 

Now it works in firefox, but in IE it outputs to the browser rather than start a download.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.