diff --git a/src/Spout/Common/Helper/GlobalFunctionsHelper.php b/src/Spout/Common/Helper/GlobalFunctionsHelper.php index 1d660c1..dd4356e 100644 --- a/src/Spout/Common/Helper/GlobalFunctionsHelper.php +++ b/src/Spout/Common/Helper/GlobalFunctionsHelper.php @@ -252,6 +252,17 @@ class GlobalFunctionsHelper header($string); } + /** + * Wrapper around global function ob_end_clean() + * @see ob_end_clean() + * + * @return void + */ + public function ob_end_clean() + { + ob_end_clean(); + } + /** * Wrapper around global function iconv() * @see iconv() diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index e5f9ad7..46f47cc 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -121,6 +121,10 @@ abstract class AbstractWriter implements WriterInterface $this->filePointer = $this->globalFunctionsHelper->fopen('php://output', 'w'); $this->throwIfFilePointerIsNotAvailable(); + // Clear any previous output (otherwise the generated file will be corrupted) + // @see https://github.com/box/spout/issues/241 + $this->globalFunctionsHelper->ob_end_clean(); + // Set headers $this->globalFunctionsHelper->header('Content-Type: ' . static::$headerContentType); $this->globalFunctionsHelper->header('Content-Disposition: attachment; filename="' . $this->outputFilePath . '"');