Data Export Grabber

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I would like to offer the users of my web application the ability to
export data in the csv format.  What I have come up with currently is
writing the stream of data into a csv file which people can right click
and download.
The downside of this, however, is that the file stays on the server and
theoretically and anybody could find the URL to the csv file and
download it.

Do you know any better way of doing this?  I have found one potential
method, but it only works on firefox:


Re: Data Export Grabber wrote:
Quoted text here. Click to load it

Yes.  Have a PHP script pose as a CSV file.  Since you
provided no details as to what kind of data you are
going to be exporting, here's a generic example of
exporting data from MySQL:

$query = 'SELECT * FROM my_table';
$result = mysql_query($query)
  or die('Query failed: ' . mysql_error());
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=data.csv');
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $comma = false;
  foreach ($line as $field) {
    if (is_numeric($field)) {
      $output = $field;
    } else {
      $output = "'" . str_replace("'", "\'", $field) . "'";
    if ($comma) {
      $output = ',' . $output;
    } else {
      $comma = true;
    echo $output;
  echo "\r\n";


Site Timeline