Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:13+00:00 2026-05-27T18:44:13+00:00

Thanks to the users community on this forum, I wrote a very simple web

  • 0

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.

I have now two functions whereby the text files returned by the search are compressed into a ZIP. Here’s my 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 .= '<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("/bin/rm -f SearchResult.zip;/usr/bin/zip -9 SearchResult.zip ". $ZIPresult ." > /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...
    }

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.

I suspect my headers are incorrectly set but I am not sure where.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:44:14+00:00Added an answer on May 27, 2026 at 6:44 pm

    Using Base64 was actually not working for reasons well stated here. Instead, I turned zlib compression off and reverted back to using binary as output format. Finally, I set Content-Type to application/octet-stream in my header. Everything is working fine now ; here’s my code:

    function getFiles() {
         ini_set('zlib.output_compression', 'Off');  
         $result = null;
         $ZIPresult = null;
         $cleanup = null;
         $output = null;
         $fileName = null;
        //remove old zip if any
         $cleanup = shell_exec("/bin/rm -f SearchResult.zip");
         error_log("SHELL OUTPUT=>" . $cleanup, 0);
         //test
         if (empty($_POST['DBRIDs'])) { return null; }
         $mydir = MYDIR; // set from the CONSTANT 
         $dir = opendir($mydir);
         $DBRIDs = $_POST['DBRIDs'];
         $getfilename = mysql_query("select /*! SQL_CACHE */ filename from automation 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>";
        $output = shell_exec("/usr/bin/zip SearchResult.zip ". $ZIPresult ." ");
        error_log("SHELL OUTPUT=>" . $output, 0);
        $fileName = 'SearchResult.zip';
        error_log("ZIP FILENAME=>" . $fileName, 0);
    
        if (file_exists($fileName)) {
        //header for forced download
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($fileName));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($fileName));
            ob_clean();
            flush();
            readfile($fileName);
            exit;
            }
        }
    
        return $result;
        }
    

    Thanks to all for taking the time!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this regex thanks to another wonderful StackOverflow user /(?:-\d+)*/g I want it
I've been full of questions lately, but thanks to this awesome community, I'm learning
I would like to thank all the users in this community for helping me
Despite that I’m a regular reader of this great forum, this is my first
I understand that this is a very broad question, but a short it depends
So I'm working on a community website/forum in PHP and I'm thinking that depending
First off thanks to all the users who have made my android developing adventure
I'm almost there with something thanks to the help of stackoverflow users. I just
how can I show users success/error messages without creating a node for it? Thanks
Thanks to all that responded to my previous thread. There is still a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.