Friday, May 31, 2013

Save output as CSV file and ask for download using PHP

<?php
header("Content-Type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; Filename="Selected_Resumes.csv"');

$outstream = fopen("php://output",'w');  //Directly ask for download with out saving
//$outstream = fopen('bath/list.csv', 'w'); // Save file on sever then ask for download

fputcsv($outstream, $titles);

foreach ($result as $data) {
    fputcsv($outstream, $data);
}
fclose($outstream);

//unlink('bath/list.csv');  // Use this to delete file, if you are using save file on server.
?>

Note: Better we can directly ask for download output as CSV, with out saving them in server.

No comments:

Post a Comment

Learn JavaScript - String and its methods - 16

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>String and it's methods - JS&l...