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