Download multiple zip file - php

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);
}

Related

PHP download multiple files as zip error

I tried to make an download button to zip my files. When I set multiple files in the array it gives the error
When I hardcode "files/path/to/8717953176714.jpg" it works.
Here is my code:
<?php
//print_r($_POST["foto"]);
$files = array($_POST["foto"]);
$random = rand(1000000000, 9999999999);
$zipname = 'file'.$random.'.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
$filename = $zipname;
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-length: ' . filesize($filename));
readfile($filename);
$file = fopen('iplog.txt', 'a', 1);
$ipz = getenv("REMOTE_ADDR");
$text = "$ipz\n";
fwrite($file, $text);
fclose($file);
?>
try this change :
$zipname =getcwd()'/file'.$random.'.zip';
The problem was. I made an array into an array. So it didn't get the good path.
$files = $_POST["foto"];
instead of
$files = array($_POST["foto"]);

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

Zip google cloude storage file damage

I use the following code to zip the google file, and the zip file is created successful, but after double clicked show error message as screenshot. How to I solve it?
$zip = new ZipArchive();
$zipname = time() . ".zip"; // Zip name
$zip->open($zipname, ZipArchive::CREATE);
$files = array('http://lh3.googleusercontent.com/k8tmo5m0KZLJbZjLiSMHjefsjJXfQSS_r6julCj8LCW-OsOFf715YOlWvHE9Xze4Rm2gxsWhl9OCLLHsXyluyDFz4n1xRQHOijS3iOs',
'http://lh3.googleusercontent.com/hk_y_5BKSCJpNOqpCkq7mWTPzljo8kXmLf2qQR16K6HASfEeDu0yxNBcheFyjnFUCMOl6-fZiJlp4n4Kb_fhihUGyn58TUDRD1iECrur');
foreach ($files as $file) {
$zip->addFromString(basename($file), file_get_contents($file));
}
$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;

Change storage folder when create zip

I use this code to create ZIP file from PHP
ZIP is correctly created, but I have a little trouble when download zip.
On browser, ZIP name appear like this
.._.._storage_temp_2013-09-03-1378245354.zip
I'm like to mantain only
2013-09-03-1378245354.zip
Here code I use:
$files = array($frente, $verso);
//$zipname = '../../storage/file.zip';
$zipname = "../../storage/temp/".date('Y-m-d')."-".time().".zip"; // Zip name
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);
//$zip->addFromString(basename($file), file_get_contents($file));
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
ob_clean();
flush();
readfile($zipname)
I have solved in this way:
$new_zipname = substr($zipname,strrpos($zipname,'/') + 1);
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$new_zipname);

Categories