title: Sending PHP headers returns "502 Bad Gateway - The server returned an invalid or incomplete response."
body: <p>My php is getting kinda rough. So I apologize in advance if I'm asking something simple.</p>
  
  <p>I'm migrating my site to a new hosting company and the following script that should start a binary (<code>.zip</code>) file download in a client's web browser:</p>
  
  <pre><code>$handle = fopen($name, 'r');
  if($handle !== false)
  {
      $length = filesize($name);
  
      header("Cache-Control: no-cache;\n");
      header("Pragma: no-cache;\n");
      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT;\n");
      header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT;\n");
      header("Content-Type: application/octet-stream;\n");
      header("Content-Transfer-Encoding: Binary;\n");
      header("Accept-Ranges: bytes;\n");
      header("Content-Length: $length;\n");
      header("Content-Disposition: attachment; filename=\"".$file_name_and_ext_only."\";\n\n");
  
      fpassthru($handle);
  
      fclose($handle);
      exit();
  }
  </code></pre>
  
  <p>gives me the following error (tested in Google Chrome):</p>
  
  <blockquote>
    <p><strong>502 Bad Gateway</strong></p>
    
    <p>The server returned an invalid or incomplete response.</p>
  </blockquote>
  
  <p>I checked, the <code>$name</code> variable refers to an existing <code>.zip</code> file on the server of a non-zero length. From what I can tell, the issue happens when I try to output the headers.</p>
  
  <p>The weird thing is that it used to work in the old hosting company.</p>
  
  <p>Any idea what I'm doing wrong here?</p>