【问题标题】:PHP - browser crash, browser is unable to output as CSVPHP - 浏览器崩溃,浏览器无法输出为 CSV
【发布时间】:2016-07-26 10:01:31
【问题描述】:

我有一个csv.phtml,我有所有行和列的值

csv.phtml:

ID,column1,column2
1,french_words,dutch words
2,french_words,dutch words
10000,randrom words, random words

php:

$html = $this->view->render('print/csv.phtml');  
// this works, it prints $html       
//echo $html; 
//exit;     

// from here: it does not work 
$this->getResponse()
        ->setHeader('Content-Disposition', 'attachment; filename=report.csv')
        ->setHeader('Content-Type', 'application/csv')
        ->setBody($html)
        ->sendResponse();
exit;

我该如何解决? $html 中是否有一些奇怪的字符导致应用程序/csv 无法呈现?我怎样才能准确地找到那个无效的部分?

【问题讨论】:

  • 看起来你的服务器已经崩溃了。在堆栈溢出时,您需要提供更多信息并更笼统地回答您的问题,因为他们需要帮助社区......而不仅仅是您自己。
  • 尝试从 curl 访问该 URL,并查看它的响应。 $ curl -i http://hide-it.com/report 查看服务器的错误日志。这不是“浏览器崩溃”,而是浏览器告诉您服务器没有响应。
  • 日志中有什么?检查 apache|nginx 日志。如果您使用的是框架(例如 Symfony),请同时检查框架日志。
  • 这怎么可能是服务器问题,我在同一台服务器上拥有同一张表上的所有其他报告,除了这个是 /report 控制器之外,它们都工作正常
  • 还要检查响应本身(chrome 检查器工具或立即 curl)。

标签: php google-chrome csv


【解决方案1】:

试试这个

$response = $this->getResponse();
$response->setContent($html);

$headers = $response->getHeaders();
$headers->clearHeaders()
    ->addHeaderLine('Content-Type', 'application/csv')
    ->addHeaderLine('Content-Disposition', 'attachment; filename=report.csv')
    ->addHeaderLine('Content-Length', strlen($html));


return $this->response;

【讨论】:

  • 这不是问题的原因。请看@deceze,他实际上解决了这个问题。这是一个服务器 php.ini 设置。
猜你喜欢
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 2021-01-15
  • 2012-06-07
  • 2014-03-09
相关资源
最近更新 更多