Data Export Grabber

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bigoxygen@gmail.com

    Data Export Grabber

    Hi.
    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:


    Thanks

  • NC

    #2
    Re: Data Export Grabber

    bigoxygen@gmail .com wrote:[color=blue]
    >
    > 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?[/color]

    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($qu ery)
    or die('Query failed: ' . mysql_error());
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename=data.c sv');
    while ($line = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
    $comma = false;
    foreach ($line as $field) {
    if (is_numeric($fi eld)) {
    $output = $field;
    } else {
    $output = "'" . str_replace("'" , "\\'", $field) . "'";
    }
    if ($comma) {
    $output = ',' . $output;
    } else {
    $comma = true;
    }
    echo $output;
    }
    echo "\r\n";
    }

    Cheers,
    NC

    Comment

    Working...