HTML文件在一个文件中下载两次

时间:2012-12-18 02:35:04

标签: php symfony

我的控制器中有以下代码:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = 'myfile.html';
$contentType = finfo_file($finfo, $file);
finfo_close($finfo);

$response = new Response();
$response->headers->set('Content-Description', 'File Transfer');
$response->headers->set('Content-Type', $contentType);
$response->headers->set('Content-Disposition', 'attachment; filename='.basename($file));
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Expires', '0');
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Pragma', 'public');
$response->headers->set('Content-Length', filesize($file));

$response->setContent(file_get_contents($file));
$response->send();
return $response;

当我在我的开发计算机(使用PHP 5.3.15的Mac)上运行此代码时,我得到了我想要的文件及其内容。但是当我在我的测试服务器(使用PHP 5.3.10的Ubuntu 12.04)上运行它时,我仍然得到该文件,但内容加倍(我在一个文件中得到两倍的文件)。

我找到了一种使用$contentType = 'application/octet-stream';让它工作的方法 我做错了什么?

1 个答案:

答案 0 :(得分:2)

我相信这两行做同样的事情(在Symfony中)

$response->send();
return $response;

我建议删除$ response-> send()行。