3
Hello, I need to compact files and folders together, for example, I have a folder called teste
and it has several files and subfolders (with more subfolders inside), and I would like to compress all this with php.
So far I only have this code, but it compacts the whole way and not just where I indicated, for example, if I indicate to it compress teste/teste1/
it won’t compress what’s inside teste1
and yes the whole way from teste
.
$directory = DIR_ARQUIVOS.'yNPA8Bg0GFLV';
$zipfile = DIR_ARQUIVOS.'yNPA8Bg0GFLV.zip';
$filenames = array();
function browse($dir)
{
global $filenames;
if ($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
{
if($file != "." && $file != ".." && is_file($dir.'/'.$file) && $dir != './arquivos/yNPA8Bg0GFLV')
{
echo $dir. "<br/>";
$filenames[] = $dir.'/'.$file;
}
else if ($file != "." && $file != ".." && is_dir($dir.'/'.$file))
{
browse($dir.'/'.$file);
}
}
closedir($handle);
}
return $filenames;
}
browse($directory);
$zip = new ZipArchive();
if ($zip->open($zipfile, ZIPARCHIVE::CREATE)!== TRUE){exit("cannot open <$zipfile>\n");}
foreach ($filenames as $filename)
{
echo "Adding " . $filename . "<br/>";
$zip->addFile($filename,$filename);
}
$zip->close();