From 8bb42ebc23e07762a421d87df3d57c4a5dbd3906 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 12 Apr 2016 10:25:08 -0700 Subject: [PATCH] Check file handle is valid before manipulating it This fixes issues when something went wrong on reader/writer init and the developer wants to close the reader/writer. The file handle may not be defined so we need to add a check for it, before actually using it. --- src/Spout/Writer/ODS/Internal/Worksheet.php | 4 ++++ src/Spout/Writer/XLSX/Helper/SharedStringsHelper.php | 4 ++++ src/Spout/Writer/XLSX/Internal/Worksheet.php | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/Spout/Writer/ODS/Internal/Worksheet.php b/src/Spout/Writer/ODS/Internal/Worksheet.php index 3a65726c..f0544f06 100644 --- a/src/Spout/Writer/ODS/Internal/Worksheet.php +++ b/src/Spout/Writer/ODS/Internal/Worksheet.php @@ -218,6 +218,10 @@ protected function getCellContent($cellValue, $styleIndex, $numTimesValueRepeate */ public function close() { + if (!is_resource($this->sheetFilePointer)) { + return; + } + fclose($this->sheetFilePointer); } } diff --git a/src/Spout/Writer/XLSX/Helper/SharedStringsHelper.php b/src/Spout/Writer/XLSX/Helper/SharedStringsHelper.php index a17493a9..9d9639db 100644 --- a/src/Spout/Writer/XLSX/Helper/SharedStringsHelper.php +++ b/src/Spout/Writer/XLSX/Helper/SharedStringsHelper.php @@ -88,6 +88,10 @@ public function writeString($string) */ public function close() { + if (!is_resource($this->sharedStringsFilePointer)) { + return; + } + fwrite($this->sharedStringsFilePointer, ''); // Replace the default strings count with the actual number of shared strings in the file header diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 3ffd97b6..af3fbc41 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -178,6 +178,10 @@ public function addRow($dataRow, $style) */ public function close() { + if (!is_resource($this->sheetFilePointer)) { + return; + } + fwrite($this->sheetFilePointer, ''); fwrite($this->sheetFilePointer, ''); fclose($this->sheetFilePointer);