1

I have a PHP file dataAPI.php which returns an array of data that is either serialized as JSON or (as I'm trying to implement) CSV. dataAPI.php is accessed exclusively through AJAX calls, which has worked fine so far with JSON data.

The problem is, I want to also have an export button on the client-side, which when clicked, would send another AJAX call to return the same data serialized in the CSV format. I know I can't send files over AJAX, so how would I go about doing this?

I've thought of creating CSV files on the server side, and then sending a redirect-url as the response to my AJAX request. If I did this though, how could I prevent two requests overwriting each others files and remove already accessed/old csv files? Is there a better way? Any help is appreciated.

theabraham
  • 15,840
  • 9
  • 42
  • 41

3 Answers3

0

You could try returning JSON in either case, except that the CSV version would return a JSON object that has one value - a long string containing the CSV data.

Matt McHugh
  • 3,995
  • 1
  • 15
  • 8
  • I'm not well-versed in PHP, but the only way to transform an array to the CSV format I see is to output it to a file using `fputcsv()`. Is there a way to turn it into a string directly? – theabraham Aug 25 '11 at 14:17
0

1) For the AJAX call, create the file somewhere on the server and send a link back - this link can then be either redirected to (e.g. in a subwindow), or clicked on...

2) To prevent data being overwritten by different requests, create an unique filename server-side (e.g. with the uniq_id() function). You can still send them with the same filename with a header() tag. Make sure you delete those files once you are sure you no longer need them (I personally use a housekeeping script like the following:

$timeout = 43200; // Max age in seconds
$cleanPaths = array("ps_files/"); // The directory to autoclean

foreach ($cleanPaths as $URLPath) {
    if ($handle = opendir($URLPath)) {
        while (false !== ($file = readdir($handle))) {
            if ((substr($file, 0, 1) !== ".") && ((time() - filectime($URLPath . $file)) >= $timeout)) {
                unlink($URLPath . $file);
            }
        }
    }
}
ty812
  • 3,293
  • 19
  • 36
0

You can put the following code in a file and then send an xhr request to it invoking the file. Or you can try opening the file in a separate tool less pop up.

$result = mysql_query("SHOW COLUMNS FROM `$table`");
$i = 0;
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
       $csv_output .= $row['Field'].", ";
       $i++;
    }
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM `$table`");
while ($rowr = mysql_fetch_row($values)) {
    for ($j=0;$j<$i;$j++) {
        $csv_output .= $rowr[$j].", ";
    }
    $csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
pyy
  • 893
  • 1
  • 8
  • 22
swordfish
  • 4,899
  • 5
  • 33
  • 61