From 4002d688e9f2854202ea615eba3dc17584284f80 Mon Sep 17 00:00:00 2001 From: aVadim Date: Sun, 16 Jun 2024 20:04:36 +0300 Subject: [PATCH] https://github.com/aVadim483/fast-excel-templator/issues/9 --- src/FastExcelTemplator/Excel.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/FastExcelTemplator/Excel.php b/src/FastExcelTemplator/Excel.php index 788e984..d433d93 100644 --- a/src/FastExcelTemplator/Excel.php +++ b/src/FastExcelTemplator/Excel.php @@ -230,8 +230,24 @@ public function save(?string $fileName = null, ?bool $overWrite = true): bool */ public function download(string $name = null) { + $tmpFile = $this->excelWriter->writer->makeTempFileName(uniqid('xlsx_writer_')); + $this->save($tmpFile); + if (!$name) { + $name = basename($tmpFile) . '.xlsx'; + } + else { + $name = basename($name); + if (strtolower(pathinfo($name, PATHINFO_EXTENSION)) !== 'xlsx') { + $name .= '.xlsx'; + } + } + + header('Cache-Control: max-age=0'); + header('Content-Type: application/vnd.ms-excel'); + header('Content-Disposition: attachment; filename="' . $name . '"'); - $this->excelWriter->download($name); + readfile($tmpFile); + unlink($tmpFile); } /**