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

  • Home
  • SEARCH
  • 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 6971721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:52:34+00:00 2026-05-27T16:52:34+00:00

I wrote a very simple web form that allows my user to view text

  • 0

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.

  • 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-27T16:52:35+00:00Added an answer on May 27, 2026 at 4:52 pm

    It might be an encoding problem. Try encoding it with Base64.

    Header:
    header('Content-Transfer-Encoding: base64');

    Then, to throw the file base64_encoded()ed to the browser, try 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.
    

    Note that simply base64_encode(readfile($fileName)) won’t work because readfile() outputs data but doesn’t return it.

    Hope it helps!

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

Sidebar

Related Questions

I wrote a very simple web form that allows my user to view text
I am creating a very simple web part that accepts entry on a form
I am writing an app that’s a very simple web service client. I have
I wrote a simple web app to let user input data as they walk
I have a very simple web application that have to read and write in
I'm absolute beginner in web technologies. I know that my question is very simple,
I wrote a simple webform that will take the user's information (via POST) and
Thanks to the users community on this forum, I wrote a very simple web
This is a very simple bash script I wrote: #!/bin/bash ITEM_LIST=items.txt LOG_FILE=log.log TOTAL_ITEMS=$(wc -l
I am experiencing some very odd timing behavior from a function I wrote. If

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.