From 11499aad9ae44409b81245e9f489a66f1fa6599e Mon Sep 17 00:00:00 2001 From: basbl Date: Mon, 16 Dec 2019 09:38:41 +0100 Subject: [PATCH] Add resource parameter handling to html, csv and xls writers --- src/PhpSpreadsheet/Writer/Csv.php | 14 +++++++++++--- src/PhpSpreadsheet/Writer/Html.php | 9 +++++++-- src/PhpSpreadsheet/Writer/Xls.php | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Csv.php b/src/PhpSpreadsheet/Writer/Csv.php index 1166bd25..1cf64634 100644 --- a/src/PhpSpreadsheet/Writer/Csv.php +++ b/src/PhpSpreadsheet/Writer/Csv.php @@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Writer; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; class Csv extends BaseWriter { @@ -77,7 +78,7 @@ class Csv extends BaseWriter /** * Save PhpSpreadsheet to file. * - * @param string $pFilename + * @param resource|string $pFilename * * @throws Exception */ @@ -92,9 +93,14 @@ class Csv extends BaseWriter Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); // Open file - $fileHandle = fopen($pFilename, 'wb+'); + if (is_resource($pFilename)) { + $fileHandle = $pFilename; + } else { + $fileHandle = fopen($pFilename, 'wb+'); + } + if ($fileHandle === false) { - throw new Exception("Could not open file $pFilename for writing."); + throw new WriterException("Could not open file $pFilename for writing."); } if ($this->excelCompatibility) { @@ -126,6 +132,8 @@ class Csv extends BaseWriter } // Close file + rewind($fileHandle); + fclose($fileHandle); Calculation::setArrayReturnType($saveArrayReturnType); diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 37d68e91..91beedb7 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -146,7 +146,7 @@ class Html extends BaseWriter /** * Save Spreadsheet to file. * - * @param string $pFilename + * @param resource|string $pFilename * * @throws WriterException */ @@ -164,7 +164,12 @@ class Html extends BaseWriter $this->buildCSS(!$this->useInlineCss); // Open file - $fileHandle = fopen($pFilename, 'wb+'); + if (is_resource($pFilename)) { + $fileHandle = $pFilename; + } else { + $fileHandle = fopen($pFilename, 'wb+'); + } + if ($fileHandle === false) { throw new WriterException("Could not open file $pFilename for writing."); } diff --git a/src/PhpSpreadsheet/Writer/Xls.php b/src/PhpSpreadsheet/Writer/Xls.php index 6932eb1a..95693dde 100644 --- a/src/PhpSpreadsheet/Writer/Xls.php +++ b/src/PhpSpreadsheet/Writer/Xls.php @@ -115,7 +115,7 @@ class Xls extends BaseWriter /** * Save Spreadsheet to file. * - * @param string $pFilename + * @param resource|string $pFilename * * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */