failed to delete buffer. No buffer to delete
When debugging a file export feature, it is easy to fall into the trap of assuming that because the browser downloads a file, the logic behind it was flawless. In this specific scenario, the user successfully triggers a download for dindi.xlsx, yet Excel refuses to open it with the error "file format or file extension is not valid." The root cause isn't actually a corruption within the binary data of the spreadsheet itself; rather, it lies in the output buffering layer that wraps the HTTP response.
The PHP notice Notice: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete is often dismissed as harmless noise, but in the context of generating Excel files via php://output, it signals a critical state mismatch. This error occurs when you attempt to end output buffering on a stream that has already been flushed or never had content written to it in the first place.
The provided code snippet attempts to solve this by calling ob_end_clean() twice: once immediately after populating the spreadsheet data, and again right before sending headers and writing to the output stream. This double-call strategy is fundamentally flawed and creates a race condition between the buffering state and the actual file generation process.
When the script executes, PHP initializes an internal output buffer. The first call to ob_end_clean() attempts to flush this buffer. At that moment, no HTML or plain text has been echoed yet; the script is still preparing to send binary data. Consequently, the buffer is empty. Attempting to clean an empty buffer triggers the warning you observed. While PHP allows cleaning an empty buffer without crashing the script, doing so prematurely can lead to issues if subsequent logic expects that buffer to exist for capturing output or if the stream state changes unexpectedly.
The real issue here is not just the warning, but the implication of how PHPExcel (now PHPSpreadsheet) interacts with the output. The code loads an existing file, modifies it, and then attempts to save it directly to php://output. However, the presence of ob_end_clean() before the headers are sent or before the writer saves the file suggests a misunderstanding of how PHP handles binary streams versus standard text output.
To fix this, you should remove both instances of ob_end_clean(). The logic for generating an Excel file does not require manual buffer management if you are sending headers immediately followed by save('php://output'). The writer handles the serialization of the cell data into the specific XML structure required for .xlsx files. As long as no other output (like debug echoes or console logs) has been flushed before the headers, the stream remains clean and ready for the binary payload.
Removing the manual buffer cleanup resolves the "No buffer to delete" warning and ensures that the Content-Type header is sent correctly alongside the binary content. If you must flush buffers in your application (for instance, if you have debug logging enabled), ensure that this flushing happens strictly after the headers are set and the file generation is complete, never before.
Additionally, consider the versioning of the libraries. The code uses PHPExcel classes (PHPExcel_IOFactory, Excel). These are legacy classes from the now-defunct PHPSpreadsheet project. While they still function in many environments, they are unmaintained. If you are building a new feature or refactoring existing code, migrating to the active PhpSpreadsheet library is highly recommended. The API remains largely similar, but the underlying engine is more robust and supports modern PHP versions better than the legacy PHPExcel namespace.
Finally, ensure that no other script or middleware is echoing content before this function runs. Even a stray echo or whitespace in an included file can corrupt the binary stream required for Office Open XML formats, leading to the "file format not valid" error in Excel regardless of the buffer warnings.