forked from chrismou/php-eol-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv.php
More file actions
46 lines (35 loc) · 1.11 KB
/
Csv.php
File metadata and controls
46 lines (35 loc) · 1.11 KB
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
46
<?php
class Csv {
private $fh;
protected $eol;
protected $delimiter;
protected $enclosure;
function __construct($fileName = '', $savePath = false, $eol = "\n", $delimiter = ",", $enclosure = '"') {
if ($fileName) {
$this->fileName = $fileName;
}
// NOTE: I don't think custom new lines work with direct downloads
// In these cases, you may need to save the file first then manually serve it for download
$this->eol = $eol;
$this->delimiter = $delimiter;
if ($savePath) {
$this->fh = fopen($savePath.$fileName.".csv", 'w');
} else {
$this->fh = fopen('php://output', 'w');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}.csv");
header("Pragma: no-cache");
header("Expires: 0");
}
}
function write($fields = array()) {
fputcsv($this->fh, $fields, $this->delimiter, $this->enclosure);
// Have we specified a custom EOL? If so, apply to the row
if("\n" != $this->eol && 0 === fseek($this->fh, -1, SEEK_CUR)) {
fwrite($this->fh, $this->eol);
}
}
function close() {
fclose($this->fh);
}
}