Repository navigation
Expand file tree
/
Copy pathFileWriter.php
More file actions
52 lines (46 loc) · 1.36 KB
/
Copy pathFileWriter.php
File metadata and controls
52 lines (46 loc) · 1.36 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
47
48
49
50
51
52
<?php
namespace SOB\CSV;
/**
* \PHPFUI\Tools\CSVWriter: a simple class to output a CSV file given data in an array
*
* Features:
* - User specified delimiter (default: comma)
* - Auto header row generation
* - Sparse array output where emtpy columns are empty in the output
*/
class FileWriter extends Writer
{
/**
* @var resource $out opened output file stream
*/
private $out;
/**
* Make a \CSV\File\Writer.
*
* @param string $filename to be output. If $download is true then this is the name the user will see. Avoid OS specific filespec conventions. If $download is false, then you can specify a directory or other OS specific filespec.
*/
public function __construct(string $filename, private readonly bool $download = true, string $separator = ',', string $enclosure = '"', string $escape = '\\', string $eol = "\n")
{
if ($this->download)
{
\header('Content-Type: application/csv');
\header('Content-Disposition: inline; filename="' . $filename . '"');
\header('Cache-Control: private, max-age=0, must-revalidate');
\header('Pragma: public');
$this->out = \fopen('php://output', 'w');
}
else
{
$this->out = \fopen($filename, 'w');
}
parent::__construct($this->out, $separator, $enclosure, $escape, $eol);
}
public function __destruct()
{
\fclose($this->out);
if ($this->download)
{
exit;
}
}
}