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;
}
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:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With