user2307958
user2307958

Reputation: 351

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)

Upvotes: 0

Views: 49

Answers (1)

user2307958
user2307958

Reputation: 351

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

Upvotes: 1

Related Questions