Symfony2 Remove UTF-8 BOM from controller responses

谁都会走 提交于 2019-12-11 17:57:54

问题


While creating a "force download" action I discovered that ALL responses generated by the controller are including the UTF-8 BOM (). This is not relevant for regular pages, but for downloaded files is undesirable, since JPG or ZIP are reported as corrupt for several Windows viewing softwares. So, the main goal is to remove the BOM from controller output.

Until now I've done this:

1-Use Win Grep to search for BOM chunk on every file on my site. Zero results.

2-Create a non-symfony test.php on the same web server and check output on the client. No BOM there.

Any ideas?

Thanks in advance, Z

UPDATE: Test Code 1. The resulting JPG includes the BOM.

public function downloadAction(Request $request){
    $filename= 'test.jpg';
    $response = new Response(file_get_contents($filename));
    $response->headers->set('Content-Type',mime_content_type($filename));        
    $response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');
   return $response;

}

Test Code 2. The resulting page also includes the BOM.

public function downloadAction(Request $request){
echo 'hello world.';
exit; 

}

UPDATE2: I just tried this: create a brand new symfony2 project from scratch, added a test controller/action using same IDE, and guess what: NO BOM..so, my guess is that something inside my symfony site is intercepting the response and adding the BOM chunk.


回答1:


The famous UTF-8 BOM had been added by LocalizeRoute.php twig extension under PEAR. In my particular case it seams Symfony filters all responses through this file, adding the BOM chunk to every page been delivered. Removing the BOM from LocalizeRoute.php was enough to fix the problem. Credits to nifr for suggesting the BOM-search. Used free window's tool WinGrep to run the binary search through the whole server. Thanks a lot.



来源:https://stackoverflow.com/questions/16737018/symfony2-remove-utf-8-bom-from-controller-responses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!