Jump to content

Force download corrupts ZIP file


kristo5747

Recommended Posts

I wrote a very simple web form that allows my user to view text files from within their Internet browser.

 

I implemented a feature whereby the text files returned by the search are compressed into a ZIP. Here's my 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 .= '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>';
             $ZIPresult .= basename($mydir) . '/' . $filename;
            } 
            if ($result) {
            $result = "<table><tbody><tr><td>Search Results.</td></tr> $result</table>";
           shell_exec("zip -9 SearchResult.zip ". $ZIPresult ." > /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;
            }

 

 

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.

 

What am I missing? Thanks for your input.

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

Link to comment
Share on other sites

It was an encoding problem. Tried the following:

 

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

 

Took care of it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.