Skip to content

Commit 00c34e7

Browse files
authored
Merge pull request #3488 from PHPOffice/Examples-General-Improvements
Examples general improvements
2 parents 523a6b6 + 78c09b0 commit 00c34e7

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

samples/download.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
require_once __DIR__ . '/Bootstrap.php';
4+
5+
use PhpOffice\PhpSpreadsheet\Helper\Downloader;
6+
use PhpOffice\PhpSpreadsheet\Helper\Sample;
7+
8+
$filename = basename($_GET['name']);
9+
$filetype = $_GET['type'];
10+
11+
try {
12+
$downloader = new Downloader((new Sample())->getTemporaryFolder(), $filename, $filetype);
13+
$downloader->download();
14+
} catch (Exception $e) {
15+
die($e->getMessage());
16+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Helper;
4+
5+
use PhpOffice\PhpSpreadsheet\Exception;
6+
7+
class Downloader
8+
{
9+
protected string $filepath;
10+
11+
protected string $filename;
12+
13+
protected string $filetype;
14+
15+
protected const CONTENT_TYPES = [
16+
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
17+
'xls' => 'application/vnd.ms-excel',
18+
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
19+
'csv' => 'text/csv',
20+
'pdf' => 'application/pdf',
21+
];
22+
23+
public function __construct(string $folder, string $filename, ?string $filetype = null)
24+
{
25+
if ((is_dir($folder) === false) || (is_readable($folder) === false)) {
26+
throw new Exception("Folder {$folder} is not accessable");
27+
}
28+
$filepath = "{$folder}/{$filename}";
29+
$this->filepath = (string) realpath($filepath);
30+
$this->filename = basename($filepath);
31+
if ((file_exists($this->filepath) === false) || (is_readable($this->filepath) === false)) {
32+
throw new Exception("{$this->filename} not found, or cannot be read");
33+
}
34+
35+
$filetype ??= pathinfo($filename, PATHINFO_EXTENSION);
36+
if (array_key_exists(strtolower($filetype), self::CONTENT_TYPES) === false) {
37+
throw new Exception("Invalid filetype: {$filetype} cannot be downloaded");
38+
}
39+
$this->filetype = strtolower($filetype);
40+
}
41+
42+
public function download(): void
43+
{
44+
$this->headers();
45+
46+
readfile($this->filepath);
47+
}
48+
49+
public function headers(): void
50+
{
51+
ob_clean();
52+
53+
$this->contentType();
54+
$this->contentDisposition();
55+
$this->cacheHeaders();
56+
$this->fileSize();
57+
58+
flush();
59+
}
60+
61+
protected function contentType(): void
62+
{
63+
header('Content-Type: ' . self::CONTENT_TYPES[$this->filetype]);
64+
}
65+
66+
protected function contentDisposition(): void
67+
{
68+
header('Content-Disposition: attachment;filename="' . $this->filename . '"');
69+
}
70+
71+
protected function cacheHeaders(): void
72+
{
73+
header('Cache-Control: max-age=0');
74+
// If you're serving to IE 9, then the following may be needed
75+
header('Cache-Control: max-age=1');
76+
77+
// If you're serving to IE over SSL, then the following may be needed
78+
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
79+
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
80+
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
81+
header('Pragma: public'); // HTTP/1.0
82+
}
83+
84+
protected function fileSize(): void
85+
{
86+
header('Content-Length: ' . filesize($this->filepath));
87+
}
88+
}

src/PhpSpreadsheet/Helper/Sample.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xl
132132
$callStartTime = microtime(true);
133133
$writer->save($path);
134134
$this->logWrite($writer, $path, /** @scrutinizer ignore-type */ $callStartTime);
135+
if ($this->isCli() === false) {
136+
echo '<a href="/download.php?type=' . pathinfo($path, PATHINFO_EXTENSION) . '&name=' . basename($path) . '">Download ' . basename($path) . '</a><br />';
137+
}
135138
}
136139

137140
$this->logEndingNotes();
@@ -147,7 +150,7 @@ protected function isDirOrMkdir(string $folder): bool
147150
*
148151
* @return string
149152
*/
150-
private function getTemporaryFolder()
153+
public function getTemporaryFolder()
151154
{
152155
$tempFolder = sys_get_temp_dir() . '/phpspreadsheet';
153156
if (!$this->isDirOrMkdir($tempFolder)) {
@@ -246,7 +249,10 @@ public function logWrite(IWriter $writer, $path, $callStartTime): void
246249
$callTime = $callEndTime - $callStartTime;
247250
$reflection = new ReflectionClass($writer);
248251
$format = $reflection->getShortName();
249-
$message = "Write {$format} format to <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds';
252+
253+
$message = ($this->isCli() === true)
254+
? "Write {$format} format to {$path} in " . sprintf('%.4f', $callTime) . ' seconds'
255+
: "Write {$format} format to <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds';
250256

251257
$this->log($message);
252258
}

0 commit comments

Comments
 (0)