php 未能删除缓冲区。没有要删除的缓冲区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14549110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
failed to delete buffer. No buffer to delete
提问by H Dindi
I am trying to generate an excel file with the extension .xlsx from the code below. I am able to download the file very well but when I open it with excel sheet, I receive the following warning error .. Excel cannot open the file 'dindi.xlsx' because the file format or file extension is not valid. Ensure that the file is not corrupted and that the file extension matches the format of the file. When I opened it with notepad ,the file had the following error:
我正在尝试从下面的代码中生成一个扩展名为 .xlsx 的 excel 文件。我能够很好地下载文件,但是当我用 Excel 工作表打开它时,我收到以下警告错误.. Excel 无法打开文件“dindi.xlsx”,因为文件格式或文件扩展名无效。确保文件未损坏并且文件扩展名与文件格式匹配。当我用记事本打开它时,该文件有以下错误:
Notice: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete
注意:ob_end_clean() [ref.outcontrol]:删除缓冲区失败。没有要删除的缓冲区
Below is the codeof what I'm trying to do.
下面是我正在尝试做的代码。
public function exportResults() {
$this -> load -> database();
$query = $this -> db -> query("
SELECT * FROM farm LIMIT 10");
$results = $query -> result_array();
$objPHPExcel = new Excel();
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load('./files/farmdetails.xlsx');
$objPHPExcel ->getActiveSheet()->setTitle('farmreport');
$objPHPExcel -> setActiveSheetIndex(0);
$i = 1;
foreach ($results as $result) {
$objPHPExcel -> getActiveSheet() -> SetCellValue('A' . $i, $result["name"]);
$objPHPExcel -> getActiveSheet() -> SetCellValue('B' . $i, $result["dateofcontract"]);
$objPHPExcel -> getActiveSheet() -> SetCellValue('C' . $i, $result["leasorname"]);
$objPHPExcel -> getActiveSheet() -> SetCellValue('D' . $i, $result["acre"]);
$objPHPExcel -> getActiveSheet() -> SetCellValue('E' . $i, $result["zone"]);
$i++;
echo $result["name"];
}
ob_end_clean();
$filename = "dindi.xlsx";
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $filename);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
$objWriter -> save('php://output');
$objPHPExcel -> disconnectWorksheets();
unset($objPHPExcel);
}
回答by Shoe
That error is just telling you that there was no buffer to delete. To avoid it just use:
该错误只是告诉您没有要删除的缓冲区。为了避免它,只需使用:
if (ob_get_contents()) ob_end_clean();
(check if there's an active output buffer) or:
(检查是否有活动的输出缓冲区)或:
if (ob_get_length()) ob_end_clean();
(checks if there's a non empty string in the buffer) as suggested by @Venu.
(检查缓冲区中是否有非空字符串)如@Venu 所建议的。
also you are calling ob_end_clean();two times there. And that only works with stackable buffers. From the PHP manual:
你也在ob_end_clean();那里打电话两次。这仅适用于可堆叠缓冲区。从 PHP 手册:
This function discards the contents of the topmost output buffer and turns offthis output buffering.
此函数丢弃最顶层输出缓冲区的内容并 关闭此输出缓冲。
Are you sure you don't want to just use ob_clean()?
您确定不想只使用ob_clean()吗?
回答by Sunny Sonar
Add this piece of code. Happens on live site when minifyhtml is active. Can be connected to advagg and httprl requests.
添加这段代码。当 minifyhtml 处于活动状态时,在实时站点上发生。可以连接advagg和httprl请求。
Proposed fix is to wrap ob_clean()with ob_get_length()condition.
建议的解决方法是ob_clean()用ob_get_length()条件包装。
if(ob_get_length() > 0) {
ob_clean();
}
回答by Antoine Subit
From the PHP Documentation comment section, credit to cornel at scoalaweb dot com:
在PHP 文档评论部分,感谢 scoalaweb dot com 的 Cornel:
Don't use ob_clean()or ob_end_clean()to clear white-space or other unwanted content "accidentally" generated by included files.
不要使用ob_clean()或ob_end_clean()清除包含文件“意外”生成的空白或其他不需要的内容。
Included files should not generate unwanted content in the first place.
包含的文件不应首先生成不需要的内容。
If they do, you are doing something wrong, like inserting white-space after "?>" (there should not be a "?>" at the end of PHP files), or other errors who not follow the basic syntax rules of PHP.
如果他们这样做,您做错了什么,例如在“ ?>”之后插入空格(?>PHP 文件末尾不应有“ ”),或其他不遵循PHP 基本语法规则的错误。
回答by Andriyansyah abdul maaz
Change
改变
ob_end_clean();
with this
有了这个
if (ob_get_contents()) ob_end_clean();
回答by Terry Lin
Your code doesn't have ob_start, of course there is no buffer to delete.
您的代码没有ob_start,当然没有要删除的缓冲区。
Please checkout http://php.net/manual/en/function.ob-start.php

![php 警告:PDOStatement::execute():SQLSTATE[HY093]:无效的参数号:绑定变量的数量与中的标记数量不匹配](/res/img/loading.gif)