I'm a real php newbie and i have this code that should allow to create a ziparchive containing the files in the folder where the php file is, give the archive the folder's name, save it in a different folder and download it.
$zipname = getcwd();
$zipname = $zipname.'.zip';
$zip = new ZipArchive;
$zip->open('D:/mypath/zip/'.basename($zipname).'', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
while (false !== ($entry = readdir($dir_handle))) {
if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
$zip->addFile($entry);
}
}
closedir($dir_handle);
}
else {
die('file not found');
}
$zip->close();
So far so good: the zip created has the right name ("foldername.zip") and is exactly where I want it to be, in the /zip/ folder in the root directory of the server. Now I'm stuck with this headers and I can't point the download to the correct location and name of the file
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
header('Content-Length: ' . filesize($zipname));
header("Location: /zip/$zipname");
?>
changing the last header to
header("Location: /zip/foldername.zip");
got it working, but I'm looking for a solution that doesn't force me to edit the php every time i create a new folder.
EDIT: Here's the working code
<?php
$zipname = getcwd();
$zipname = substr($zipname,strrpos($zipname,'\\')+1);
$zipname = $zipname.'.zip';
$zip = new ZipArchive;
$zip->open('D:/inetpub/webs/mydomaincom/zip/'.basename($zipname).'', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
while (false !== ($entry = readdir($dir_handle))) {
if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
$zip->addFile($entry);
}
}
closedir($dir_handle);
}
else {
die('file not found');
}
$zip->close();
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
header('Content-Length: ' . filesize($zipname));
header('Location: /zip/'.$zipname);
?>
Your last header isn't correct :
header("Location: /zip/$zipname");
It should be :
header("Location: /zip/".$zipname);
Since your $zipname is a variable and not a string