Sunday, January 12, 2014

Using php download a temporary file


<?php
$dataArray = array(
    "name" => "Pritom K Mondal",
    "company" => "Some Company"
);
$tmpName = tempnam(sys_get_temp_dir(), 'data');

$tmpFile = fopen($tmpName, 'w');
fputcsv($tmpFile, $dataArray);

header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=download.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($tmpName));

ob_clean();
flush();
readfile($tmpName);
unlink($tmpName);
?>

No comments:

Post a Comment