欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

web下载网站代码

程序员文章站 2024-02-12 10:18:34
...

1.打包代码,并直接通过浏览器下载 2.解压zip文件到服务器 PHP ?php/** * @author MarkZhao(zhaody901#126.com) */$dir = dirname(__FILE__).'/';// zip and download// zip.php?type=zipdir=subdir// unzip deflate way zipped zip file// zip.php?type=unzip

1. 打包代码, 并直接通过浏览器下载
2. 解压zip文件到服务器
PHP
open($name, ZipArchive::CREATE) === TRUE) {
		$files = map($dir.$subdir);
		foreach ($files as $file) {
			$dir  = str_replace('\\', '/', $dir);
			$file = str_replace('\\', '/', $file);
			$zip->addFile($file, '_ROOT_/' . str_replace($dir, '', $file));
		}
		$zip->close();
		down($dir.$name);
		echo 'ok';
	} else {
		echo 'failed';
	}
} else if ($type=='unzip') {

	$zip = new ZipArchive();
	if ($zip->open($name) === TRUE) {
		$zip->extractTo(dirname(__FILE__).'/');
		$zip->close();
		echo 'ok';
	} else {
		echo 'failed';
	}
}


function map($directory) {
	$files = array();
	if (file_exists($directory) && is_dir($directory)) {
		foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
			$files[] = $file->getRealPath();
		}
	}
	return $files;
}

function down($file) {
	$filename = $filename ? $filename : basename($file);
	$filesize = filesize($file);
	ob_end_clean();
	@set_time_limit(0);
	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
	} else {
		header('Pragma: no-cache');
	}
	header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
	header('Content-Encoding: none');
	header('Content-Length: ' . $filesize);
	header('Content-Disposition: attachment; filename=' . $filename);
	header('Content-Type: ' . $filetype);
	readfile($file);
	exit;
}
web下载网站代码