title: Symfony 1.4: Best way provide a file download without using a template/view
body: <p>Of cource some other people have discussed these problem on stackoverflow, but not all ansers works for me and often they do not provide a version of there symfony installation.</p>
  
  <p>Topics I read:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9900991/send-attachment-download-file-from-symfony-action">Send attachment/Download file from Symfony action</a></li>
  <li><a href="https://stackoverflow.com/questions/10894166/how-to-download-a-file-on-clicking-thefile-path-using-php-symfony">How to download a file on clicking thefile path using PHP-Symfony?</a></li>
  <li><a href="https://stackoverflow.com/questions/5218648/symfony-sethttpheader-doesnt-work-header-does">symfony: setHttpHeader() doesn&#39;t work, header() does</a></li>
  </ul>
  
  <p><strong>Thats the point for me to ask how you handle file downloads in symfony 1.4 (without using the view)?</strong> In all my use cases I need a template file to render the response. If I send the response due the controller there is the only possibility to send it without an php error (header already sent) with</p>
  
  <p><strong>controller:</strong></p>
  
  <pre><code>/** @var $response sfWebResponse */
  $response = $this-&gt;getResponse();
  $response-&gt;clearHttpHeaders();
  $response-&gt;setContentType($mimeType);
  $response-&gt;setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
  $response-&gt;setHttpHeader('Content-Description', 'File Transfer');
  $response-&gt;setHttpHeader('Content-Transfer-Encoding', 'binary');
  $response-&gt;setHttpHeader('Content-Length', filesize($filePath));
  $response-&gt;setHttpHeader('Cache-Control', 'public, must-revalidate');
  $response-&gt;setHttpHeader('Pragma', 'public');
  $response-&gt;sendHttpHeaders();
  
  readfile($filePath); die();
  </code></pre>
  
  <p>This works without an template file. But imho this is not so pretty coding.</p>
  
  <p>The alternative way with the template:</p>
  
  <p><strong>controller:</strong></p>
  
  <pre><code> /** @var $response sfWebResponse */
  $response = $this-&gt;getResponse();
  $response-&gt;clearHttpHeaders();
  $response-&gt;setContentType($mimeType);
  $response-&gt;setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
  $response-&gt;setHttpHeader('Content-Description', 'File Transfer');
  $response-&gt;setHttpHeader('Content-Transfer-Encoding', 'binary');
  $response-&gt;setHttpHeader('Content-Length', filesize($filePath));
  $response-&gt;setHttpHeader('Cache-Control', 'public, must-revalidate');
  $response-&gt;setHttpHeader('Pragma', 'public');
  $response-&gt;setContent(file_get_contents($filePath));
  $response-&gt;sendHttpHeaders();
  
  return sfView::NONE;
  </code></pre>
  
  <p><strong>view:</strong></p>
  
  <pre><code>&lt;?php echo $sf_response-&gt;getRawValue()-&gt;getContent(); ?&gt;
  </code></pre>