Home >Backend Development >PHP Tutorial >PHP online packaging_Support subdirectories_PHP tutorial
The program uses the zip extension of php. If the server supports it, you can use it ^_^. The generated zip package can be decompressed with winrar, etc., and of course it can also be decompressed with php. It will be released after a while after decompression.
$button=$_POST['button'];
if($button=="Start Packing")
{
$zip = new ZipArchive ();
$filename = "./".date("Y-m-d")."_".md5(time())."_jackfeng.zip";
if ($zip->open( $ FILENAME, Ziparchive :: Create)! == True) {
EXIT ("Unable to create & lt; $ firename & gt; n");
}
$ files = listdir (); $files as $path)
{
$zip->addFile($path,str_replace("./","",str_replace("\","/",$path)));
}
echo "Compression completed, total compressed: " . $zip->numFiles . "n files";
$zip->close();
}
Function listdir($start_dir='.') {
$files = array();
if (is_dir($start_dir)) {
$fh = opendir($start_dir);
while (( $file = readdir($fh)) !== false) {
if (strcmp($file, '..')==0 || strcmp($file, '..')==0) continue;
$filepath = $start_dir . '/' . $file;
if ( is_dir($filepath) )
$files = array_merge($files, listdir($filepath));
else
array_push($files, $filepath);
}
closedir($fh);
} else {
$files = false;
}
return $files;
}
?>