title: Force download corrupts ZIP file
body: <p>I wrote a very simple web form that allows my user to view text files from within their Internet browser.</p>
  
  <p>I implemented a feature whereby the text files returned by the search are compressed into a ZIP. Here's my code</p>
  
  <pre><code>        function getFiles() {
           $result = null;
           $ZIPresult = null;
           if (empty($_POST['DBRIDs'])) { return null; }
           $mydir = MYDIR; 
           $dir = opendir($mydir);
           $DBRIDs = $_POST['DBRIDs'];
           $getfilename = mysql_query("select filename from search_table where rid in (" . $DBRIDs . ")") or die(mysql_error());
           while ($row = mysql_fetch_array($getfilename)) {
           $filename = $row['filename'];
           $result .= '&lt;tr&gt;&lt;td&gt;&lt;a href="' . basename($mydir) . '/' . $filename . '" target="_blank"&gt;' . $filename . '&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;';
           $ZIPresult .= basename($mydir) . '/' . $filename;
          } 
          if ($result) {
          $result = "&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Search Results.&lt;/td&gt;&lt;/tr&gt; $result&lt;/table&gt;";
         shell_exec("zip -9 SearchResult.zip ". $ZIPresult ." &gt; /dev/null ");
  
  $fileName = 'SearchResult.zip';
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-Transfer-Encoding: binary");
  header('Content-type: application/zip');
  header("Content-length: " . filesize($fileName));
  header('Content-Disposition: attachment; filename="' . $fileName . '"');
  readfile($fileName); 
          } return $result;
          }
  </code></pre>
  
  <p>It works great If I download the ZIP file from the server using FTP (for example) but I force the download from the page header, the ZIP is corrupted.</p>
  
  <p>What am I missing? Thanks for your input.</p>
  
  <p><strong>PS: The new ZipArchive() library/class is not available on our production environment so I chose to use the Unix utility ZIP instead.</strong></p>