Zip folder first then allow user to download it - php

On my site I want to have a link that can zip a particular folder including its content then allow the user to download it. It will be use for downloading a bundled files.

try this code:
$files[] = base_url().'uploads/'.$names->file_name;
pass your files into this $files[] array.
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach($files as $file)
{ //echo $file; exit;
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
}
$zip->close();
header('Content-disposition: attachment; filename=project_files.zip');
header('Content-type: application/zip');
readfile($tmp_file);

Related

Download multiple zip file

I want download multiple zip for multiple image at a time.But my code crate only one zip file for all image.
foreach ($data as $key => $files) {
/*$fileName="file.zip";*/
$fileName = $key.'.zip';
$tmpFile = tempnam('/tmp', '');
$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
// download file
$fileContent = file_get_contents($file);
$zip->addFromString(basename($file), $fileContent);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$fileName);
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
echo $fileName;
unlink($tmpFile);
}

PHP zipfile won't open

For the life of me, I cannot figure out why this zip file fails to open, it errors saying it cannot open the file. The SELECT is a couple of BLOB files in phpmyadmin. Here is the code:
$uploadQuery = mysqli_query($con, "SELECT * FROM upload WHERE majorarea = 'Big Data'") or die (mysql_error());
$files = $uploadQuery;
$zipname = 'papers.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
readfile($zipname);

download multiple files as zip in php without save on host

I use this way for download multiple files as zip :
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
But the file file.zip be save on my host . I don't want to save on my host i just wanna download it withou save .
Thank

Download mp3s from url array to zip file

I have an array of links to mp3 files. I need to have all of the mp3s downloaded from a server and then zipped.
This code below is not working:
Any suggestions?
$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
echo "$file";
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The archive is either in unknown format or damaged
You first need to download those files and store them locally on your server. Once that is done you will be able to zip them and make the zip file available for download.
The most basic way of achieving this is by using PHP's file_get_contents(), file_put_contents() and parse_url().
This code has not been thoroughly tested.
// Fixed the name of the array
$audio_files = array('http://example.com/1.mp3', 'http://example.com/2.mp3');
// You need to change this to
// a path where you want to store your
// audio files (Absolute path is best)
$local_path = '/local/path/here/';
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
// Download the files
// Not an elegant way to
// download files
foreach ($audio_files as $file) {
// First let's get the file name
// by parsing the URL
// and trimming the '/'
$file_name = ltrim(parse_url($file, PHP_URL_PATH), '\/');
// Download the file
$a_contents = file_get_contents($file);
// Place the downloaded content
// in the defined local path
file_put_contents($local_path . $file_name, $a_contents);
// Add the file to archive
$zip->addFile($local_path . $file_name);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
This is a very bad way to achieve your task for the following reasons (among many others):
There is no timeout for the download process
If a file fails to download it will halt the execution
It may take a very long time (depending on the size of the audio files) to serve the zip file
There is no proper error / exception handler
Many other reasons regarding structure that I'm not gonna go through here.
try the following: use parse_url to get the actual filename, use file_get_contents and file_put_contents to download and save the data locally...
$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
echo "$file";
$parsed = parse_url($file);
$localfile = "." . $parsed["path"];
$mp3 = get_file_contents($file);
file_file_contents($localfile, $mp3);
$zip->addFile($localfile);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

Download selected files into zip format in wordpress

I am using a wordpress template page download.php and the code is :
$ck=$_REQUEST['select'];
$num=count($ck);
$zip_file_name='demodown.zip';
$zip = new ZipArchive();
if ($zip->open($zip_file_name, ZIPARCHIVE::CREATE )!==TRUE)
{
exit("cannot open <$zip_file_name>\n");
}
$file_path = $_SERVER['DOCUMENT_ROOT']."/lab4/brazil_resource/wp-content/pdf_upload/";
for($i=0;$i<$num;$i++)
{
$sql=mysql_query("select * from wp_pagecontent where id='$ck[$i]'");
$f=mysql_fetch_array($sql);
$files=$f['pdf_file'];
$zip->addFile($file_path.$files,$files);
}
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_file_name.'"');
exit;
Using the above code I cannot zip the files that are selected. The download files in showing 0 bytes size.
Please help
I had solved it now finally
$zip_file_name='demo.zip';
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
$file_path = content_url()."/pdf_upload/";
foreach($_REQUEST['select'] as $file)
{
$download_file = $file_path.$file;
$download_file2 = file_get_contents($download_file);
$zip->addFromString('files/'.$file,$download_file2);
}
$zip->close();
if(file_exists($tmp_file))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_file_name.'"');
readfile($tmp_file);
// remove zip file is exists in temp path
}
I have solved the other issue with the following code:
$zip_file_name='demo.zip';
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
for($i=0;$i<$num;$i++)
{
$sql=mysql_query("select * from wp_pagecontent where id='$ck[$i]'");
$f=mysql_fetch_array($sql);
$files=$f['pdf_file'];
$download_file = file_get_contents($files);
//$fe=$file_path.$files;
//$zip->addFromString(basename($fe),file_get_contents($fe));
$zip->addFromString(basename($files),$download_file);
}
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_file_name.'"');
header("Content-length: ".filesize($zip_file_name));
readfile($tmp_file);
exit;

Categories