title: Symfony2 - Force file download
body: <p>I'm trying to download a file when a user clicks on download link.</p>
  
  <p>In Controller:</p>
  
  <pre><code>    $response = new Response();
      $response-&gt;headers-&gt;set('Content-type', 'application/octect-stream');
      $response-&gt;headers-&gt;set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
      $response-&gt;headers-&gt;set('Content-Length', filesize($filename));
  
      return $response;
  </code></pre>
  
  <p>This is opening the dialog box to save the file, but it says the file is 0 bytes.
  And changing it to:</p>
  
  <pre><code>        $response = new Response();
          $response-&gt;headers-&gt;set('Content-type', 'application/octect-stream');
          $response-&gt;headers-&gt;set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
          $response-&gt;headers-&gt;set('Content-Length', filesize($filename));
          $response-&gt;headers-&gt;set('Content-Transfer-Encoding', 'binary');
          $response-&gt;setContent(readfile($filename));
  
          return $response;
  </code></pre>
  
  <p>I get a bunch of weird characters instead of the file download dialog box.</p>
  
  <p>Finally, switching the "setContent" line to:</p>
  
  <pre><code>    $response-&gt;setContent(file_get_contents($filename));
  </code></pre>
  
  <p>It returns a PHP error:</p>
  
  <blockquote>
    <p>Fatal error: Allowed memory size...</p>
  </blockquote>
  
  <p>Any clues on how to achieve this?
  I've done it before in PHP (wihtout MVC), but I don't know what can be missing to do it through Symfony2...</p>
  
  <p>Maybe the solution is setting the memory_limit in PHP.INI, but I guess it´s not the best practice...</p>