Form downloads same ZIP over and over
<p>Thanks to the users community on this forum, I wrote a very simple web form that allows my user to view text files from within their Internet browser.</p>

<p>I have now two functions whereby the text files returned by the search are compressed into a ZIP. Here's my code</p>

<pre><code>    function getFilesFromSite() { 
     $result = null; 
     $ZIPresult = null;
     if (empty($_POST['DBSite'])) { return null; } 
     $mydir = MYDIR;  
     $dir = opendir($mydir); 
     $DBSite = $_POST['DBSite']; 
     $getfilename = mysql_query("select filename from search_table where site='" . $DBSite . "'") 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("/bin/rm -f SearchResult.zip;/usr/bin/zip -9 SearchResult.zip ". $ZIPresult ." &gt; /dev/null ");

    //header for forced download
    header("Pragma: public");
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $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 . '"');

    ob_start(); // Starts output buffering.
    readfile($fileName); // "Outputs" the file.
    $content = ob_get_flush(); // Grabs the output and assigns it to a variable.
    print base64_encode($content);
    }

    function getFilesFromError() { 
//Just a copy paste from above with different input parameter...
    }
</code></pre>

<p>The problem is that the ZIP file with the contents from whatever search was done first gets downloaded over and over again. For instance, the results from getFilesFromSite() will always get downloaded even though I did a search with getFilesFromError() afterwards.</p>

<p>I suspect my headers are incorrectly set but I am not sure where.</p>

<p>PS: The new ZipArchive() library/class is not available on our production environment so I chose to use the Unix utility ZIP instead.</p>
