chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
 
namespace PhpOffice\PhpSpreadsheet\Helper;
 
use PhpOffice\PhpSpreadsheet\Exception;
 
/**
 * Assist downloading files when samples are run in browser.
 * Never run as part of unit tests, which are command line.
 *
 * @codeCoverageIgnore
 */
class Downloader
{
    protected string $filepath;
 
    protected string $filename;
 
    protected string $filetype;
 
    protected const CONTENT_TYPES = [
        'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'xls' => 'application/vnd.ms-excel',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        'csv' => 'text/csv',
        'html' => 'text/html',
        'pdf' => 'application/pdf',
    ];
 
    public function __construct(string $folder, string $filename, ?string $filetype = null)
    {
        if ((is_dir($folder) === false) || (is_readable($folder) === false)) {
            throw new Exception('Folder is not accessible');
        }
        $filepath = "{$folder}/{$filename}";
        $this->filepath = (string) realpath($filepath);
        $this->filename = basename($filepath);
        if ((file_exists($this->filepath) === false) || (is_readable($this->filepath) === false)) {
            throw new Exception('File not found, or cannot be read');
        }
 
        $filetype ??= pathinfo($filename, PATHINFO_EXTENSION);
        if (array_key_exists(strtolower($filetype), self::CONTENT_TYPES) === false) {
            throw new Exception('Invalid filetype: cannot be downloaded');
        }
        $this->filetype = strtolower($filetype);
    }
 
    public function download(): void
    {
        $this->headers();
 
        readfile($this->filepath);
    }
 
    public function headers(): void
    {
        // I cannot tell what this ob_clean is paired with.
        // I have never seen a problem with it, but someone has - issue 3739.
        // Perhaps it should be removed altogether,
        // but making it conditional seems harmless, and safer.
        if ((int) ob_get_length() > 0) {
            ob_clean();
        }
 
        $this->contentType();
        $this->contentDisposition();
        $this->cacheHeaders();
        $this->fileSize();
 
        flush();
    }
 
    protected function contentType(): void
    {
        header('Content-Type: ' . self::CONTENT_TYPES[$this->filetype]);
    }
 
    protected function contentDisposition(): void
    {
        header('Content-Disposition: attachment;filename="' . $this->filename . '"');
    }
 
    protected function cacheHeaders(): void
    {
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
 
        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
    }
 
    protected function fileSize(): void
    {
        header('Content-Length: ' . filesize($this->filepath));
    }
}