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 6885887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:41:27+00:00 2026-05-27T05:41:27+00:00

I have the following code running on my site. The only problem I have

  • 0

I have the following code running on my site. The only problem I have with it is that it makes a zip file on the server and then user downloads.

I would like to know what should I do in order for the zip file to be generated “on the fly” without being dumped on the server disk first. I would also like to make possible for the user to pause/resume the download.

//function for zip
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
  //create the object
  $zip = new ZipArchive();
  //create the file and throw the error if unsuccessful
  if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    exit("cannot open <$archive_file_name>\n");
  }

  //add each files of $file_name array to archive
  foreach($file_names as $files)
  {


  $zip->addFile($file_path.str_replace('./','',$files),translit($files).".mp3");
  }
  $zip->close();

  //then send the headers to foce download the zip file
  header("Content-type: application/zip");
  header("Content-Disposition: attachment; filename=$archive_file_name");
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile("$archive_file_name");
  exit;
}
  • 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-27T05:41:28+00:00Added an answer on May 27, 2026 at 5:41 am

    There are three requirements you mention:

    • zip is available for download on fly – I presume that by this, you mean “zip file is created on the fly”. This is already happening. Indeed, it is what your script does, it’s raison d’etre, if you will.
    • zip file should not be created on server – You have to create a file on the server, even if it’s only temporarily, because that is how the Zip extension works. You can delete it after the user has downloaded it (just add unlink($archive_file_name); on the line before exit;).
    • user can also resume it if paused – This requirement is (mostly) incompatible with zip file should not be created on server. Resumable downloads are implementable in PHP, but it is quite difficult to do and requires access to the Range: header of the request – which not every server will allow you to have. Also, you would have to generate the whole file for even a partial request, because you have deleted it from the server. Apache has an implementation of resumable downloads, but it requires (AFAIK) that the file be static on the hard drive, and requested directly. This would mean that deleting the file after it was downloaded (at the end of the PHP script) would break the resumability.

    Reading between the lines, I suspect the problem you are having is that your server’s hard drive space is getting used up by all the Zip archives you are creating and not deleting. The solution to this (while still allowing resumable downloads) is to implement some form of TTL checker on the server and periodically deleting files that are older than, for example, 1 day. You could do this with a cron job, or by running the check when you go to create a new arhive.

    At the moment, your code does not specify where the zip files will be created, and this is something you would need to do. Here is an example that assumes your script is in the root directory of your site, and that there is a directory called zips in the root directory of your site.

    The basic flow is:

    • Loop through the /zips directory, and delete all files that are older than 1 day.
    • Create a new archive in the /zips directory
    • Redirect the user to the path of that static file.
    function zipFilesAndDownload($file_names, $archive_file_name, $file_path) {
    
      // Archive directory
      $archiveDir = 'zips';
      // Time-to-live
      $archiveTTL = 86400; // 1 day
      // Files to ignore
      $ignoreFiles = array('.', '..');
    
    
      // Loop the storage directory and delete old files
      if ($dp = opendir($archiveDir)) {
        while ($file = readdir($dp)) {
          if (!in_array($file, $ignoreFiles) && filectime("$archiveDir/$file") < (time() - $archiveTTL)) {
            unlink("$archiveDir/$file");
          }
        }
      }
    
      // Re-format the file name
      $archive_file_name = "$archiveDir/".basename($archive_file_name);
      // Create the object
      $zip = new ZipArchive();
      // Create the file and throw the error if unsuccessful
      if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
        exit("Cannot open '$archive_file_name'\n");
      }
      // Add each file of $file_name array to archive
      foreach($file_names as $file) {
        $zip->addFile($file_path.str_replace('./', '', $file), translit($files).".mp3");
      }
      $zip->close();
    
      // Then send the headers to redirect to the ZIP file
      header("HTTP/1.1 303 See Other"); // 303 is technically correct for this type of redirect
      header("Location: http://{$_SERVER['HTTP_HOST']}/$archive_file_name");
      exit;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running the following code on a file that has been uploaded to this
I have the following code running on my Android device. It works great and
I have the following code in a jQuery JavaScript document running on a page
Following the directions at this question , I have some code running to extract
I have following code class User attr_accessor :name end u = User.new u.name =
I have an ASP.NET site that has been running perfectly for a long time,
I have the following code in my htaccess file: RewriteEngine On RewriteCond %{HTTP_HOST} !^www.example.com$
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in

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.