From 9cdbddf3bfa82fefc59052cee67bfbe08e94d49a Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sat, 16 May 2020 18:08:10 +0900 Subject: [PATCH] Early bail out if resource cannot be opened --- src/PhpSpreadsheet/Writer/Ods.php | 9 +++++++-- src/PhpSpreadsheet/Writer/Xlsx.php | 11 ++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods.php b/src/PhpSpreadsheet/Writer/Ods.php index 08bd2629..9de19eb4 100644 --- a/src/PhpSpreadsheet/Writer/Ods.php +++ b/src/PhpSpreadsheet/Writer/Ods.php @@ -33,7 +33,7 @@ class Ods extends BaseWriter private $spreadSheet; /** - * @var bool|resource + * @var resource */ private $fileHandle; @@ -105,7 +105,12 @@ class Ods extends BaseWriter } } - $this->fileHandle = fopen($pFilename, 'wb+'); + $fileHandle = fopen($pFilename, 'wb+'); + if ($fileHandle === false) { + throw new WriterException('Could not open file ' . $pFilename . ' for writing.'); + } + + $this->fileHandle = $fileHandle; } $zip = $this->createZip(); diff --git a/src/PhpSpreadsheet/Writer/Xlsx.php b/src/PhpSpreadsheet/Writer/Xlsx.php index 9c947d0a..3b4bdfd9 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx.php +++ b/src/PhpSpreadsheet/Writer/Xlsx.php @@ -108,7 +108,7 @@ class Xlsx extends BaseWriter private $drawingHashTable; /** - * @var bool|resource + * @var resource */ private $fileHandle; @@ -196,11 +196,12 @@ class Xlsx extends BaseWriter } } - $this->fileHandle = fopen($pFilename, 'wb+'); - } + $fileHandle = fopen($pFilename, 'wb+'); + if ($fileHandle === false) { + throw new WriterException('Could not open file ' . $pFilename . ' for writing.'); + } - if (!is_resource($this->fileHandle)) { - throw new WriterException('Could not open resource for writing.'); + $this->fileHandle = $fileHandle; } $saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog();