/
csv.php
45 lines (36 loc) · 1.08 KB
/
csv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
function createCSV(array &$array)
{
// only continue when there is data
if (count($array) == 0) {
return null;
}
// store output in a internal buffer
ob_start();
// create file stream
$df = fopen("php://output", 'w');
// write csv headers (firstName, lastName etc) to filestream
fputcsv($df, array_keys(reset($array)));
// iterate subsequent rows and write data
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
// gets the current buffer contents and delete current output buffer.
return ob_get_clean();
}
function setHeaders($filename) {
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
// get the data from the query parameters
$query_parameters = array ($_GET);
setHeaders("linkedin-csv-export.csv");
echo createCSV($query_parameters);
die();
?>