title: The file can not be opened, downloaded or is corrupted when forcing its download
body: <p>This is how my webhosting scheme looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/xrDPS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xrDPS.png" alt="enter image description here"></a></p>
  
  <p>This is the code I'm having problems with, trying to force a file to be downloaded:</p>
  
  <pre><code>$fetch_file = $set_query-&gt;fetch_assoc();
  $file = "../library/".$fetch_file["file_name"];
  if (file_exists($file) &amp;&amp; is_file($file)) {
  if(ini_get('zlib.output_compression')) {       
      ini_set('zlib.output_compression', 'Off'); 
  }
  header('Content-Length: ' . filesize($file));
  header('Pragma: public');
  header('Expires: 0');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename=\"'.basename($file).'\"');
  header('Content-Transfer-Encoding: binary');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Connection: close');
  ob_end_flush();
  ob_end_clean();
  readfile($file);
  exit;
  </code></pre>
  
  <p>The download dialog actually pops up but I get an error from my browser saying that it was unable to read, and as a result, unable to open or download.</p>
  
  <p>I tried to put <code>ob_end_flush()</code> and <code>ob_end_clean()</code> before the headers but it didn't work out. I don't know why the download dialog properly shows the file to download but says it can't be read nor opened.</p>
  
  <p>I managed to fix that by using:</p>
  
  <pre><code>// digital_lub/JKAJSD831.pdf
  $file = UPLOAD_PATH.$fetch_file["file_name"];
  </code></pre>
  
  <p>The dialog opens and downloads the file now but it won't open locally, saying it's corrupted. When I open it as text, it contains the HTML of my page.</p>
  
  <p>My download page is requested by being included in <code>index.php</code> via an <code>.htaccess</code> url rewrite. This is <code>index.php</code>:</p>
  
  <pre><code>&lt;?
  require_once("constructor.php");
  $smarty-&gt;display("page_index");
  if (isset($_GET["page"])) {
  
      $page = htmlspecialchars($_GET["page"]);
      $setPageFile = "module/".$page.".php";
  
      if (file_exists($setPageFile)) {
          include($setPageFile);
      } else {
          $smarty-&gt;assign("alert", array(1, "Żądany moduł nie jest obsługiwany."));
      }
  } else {
      Header("Location: login");
  }
  $smarty-&gt;display("page_footer");
  ?&gt;
  </code></pre>