title: PHP readfile() corrupts file
body: <p>In my function I'm downloading a file that saves downloads to a log file. Whenever I try to download an Excel file I uploaded, Excel states that it is corrupted. My local copy works fine. Here is my download.php:</p>
  
  <pre><code>&lt;?php
  include_once 'includes/db_connect.php';
  include_once 'includes/functions.php';
  
  sec_session_start();
  ob_start();
  ?&gt;
  
  &lt;?php if (login_check($mysqli) == true) :?&gt;
  &lt;?php
  $logFile = "download.log";
  $directory = "./downloads/";
  date_default_timezone_set('America/New_York');
  
  $filename = $_GET['file'];
  $path = "$directory$filename";
  if(file_exists($path) AND substr_count($filename,"/") == "0") {
    if (isset($logFile)) {
      $downloadLogRecord = $filename." || ".$_SESSION['name']." || ".$_SESSION['username']." || ".$_SERVER['REMOTE_ADDR']." || ".date('Y-m-d H:i:s')."\r\n";
      @file_put_contents($logFile,$downloadLogRecord,FILE_APPEND|LOCK_EX);
    }
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=$filename"); 
    header("Content-Length: ".filesize($path));
    readfile("$path");
  }
  ?&gt;
  &lt;?php else : ?&gt;
    &lt;p&gt;
      &lt;span class="error"&gt;You are not authorized to access this page.&lt;/span&gt; Please &lt;a href="index.php"&gt;login&lt;/a&gt;.
    &lt;/p&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p>How can I fix this?</p>