Reputation: 55
I have a script for create zip with images. It works really well !
create_zip($files_to_zip, 'zip/download-'.$id_account.'-'.$time.'.zip');
function create_zip($files = array(),$destination = '',$overwrite = true) {
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
$zip->close();
return file_exists($destination);
}
else { return false; }
}
When I download the file, it's well named zip/download-'.$id_account.'-'.$time.'.zip
But when I want to extract this, the extract folder is always named files
.
result
Please do you have any idea for rename my unzipped file to an another name ?
Thanks a lot !
Upvotes: 2
Views: 1935
Reputation: 4894
You can use this code to create you zip file.
do like this
$File = 'path' . time() . '.zip';
$zip = new \ZipArchive();
$res = $zip->open($File, \ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile($filename);
$zip->close();
}
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Length: " . filesize($File));
header("Pragma: no-cache");
header("Expires: 0");
readfile($File);
header("Connection: close");
In your case you just missed header part.
Just add the header part in your code then it will work for you.
Upvotes: 1