开发者

Download PHP array as a file

开发者 https://www.devze.com 2023-02-13 01:37 出处:网络
I have an array with 3 attributes. I want to create a downloadable link for a file which has the array in a tab-delimited format.

I have an array with 3 attributes. I want to create a downloadable link for a file which has the array in a tab-delimited format.

ATTR1     ATTR2     ATTR3
23.7      45.89     1.09
....      .....   开发者_如何学运维  ....
....      .....     ....
....      .....     ....


$headings = array('ATTR1','ATTR2','ATTR3');

$fp = fopen('file.csv', 'w');

fputcsv($fp,$headings,"\t");
foreach($array as $row) {
   fputcsv($fp,$row,"\t");
}

fclose($fp);


You should output content like ususal and set addition header (before you output any content).

<?php
header("Content-Disposition", 'attachment, filename="NAMEOFFILE.tsv"')

// output content in tab delimited format below
?>

And then point your link to this script. When user hit it - download window appear (or file will be opened in application, depends on browser settings).

0

精彩评论

暂无评论...
验证码 换一张
取 消