I'm very new to php and I just made my first script, which is working fine, but is missing a final touch.
The script zips all files in the folder containing the php and creates a downloadable archive.
Here's the code
<?php
$zipname = 'test.zip';
$zip = new ZipArchive;
$zip->open('D://mypath//zip//$zipname', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
while (false !== ($entry = readdir($dir_handle))) {
if ($entry != "." && $entry != ".." && !strstr($entry,'.php') && !strstr($entry,'.zip')) {
$zip->addFile($entry);
}
}
closedir($dir_handle);
}
else {
die('file not found');
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=$zipname");
header('Content-Length: ' . filesize($zipname));
header("Location: $zipname");
?>
What I'd like to achieve is having $zipname = "the name of the folder.zip"
So, if the php is inside "/mypath/blablabla/" i want my zip $zipname to be "blablabla.zip"
Any help will be appreciated!
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);
?>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…