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);
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'm trying to create a zip archive from some files using PHP.
$zipname = 'path/to/my/dir/' . uniqid('MyZip', true) . '.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$dl_file = file_get_contents($file);
$zip->addFromString(basename($file),$dl_file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
unlink($zipname);
It creates an archive just fine in the correct location named something to the extent of: MyZip124153.zip
However, the file my browser downloads is named path-to-my-dirMyZip124153.zip.
I'm not sure how I can prevent it from doing this. I'd prefer to not have this filesystem info embedded in these filenames.
Thank you!
Obviously it is going to put the path inside a file name , as you are using the path string as filename inside the header.
Change your :
header('Content-disposition: attachment; filename=' . $zipname);
to something like :
$new_file_name = 'myzipfile.zip';
header('Content-disposition: attachment; filename=' . $new_file_name);
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);
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;