php web下载网站代码
web下载网站代码 1. 打包代码, 并直接通过浏览器下载2. 解压zip文件到服务器[代码片段(75行)]
-
打包代码, 并直接通过浏览器下载
-
解压zip文件到服务器
<?php
/**
* @author MarkZhao(zhaody901#126.com)
*/
$dir = dirname(__FILE__).'/';
// zip and download
// zip.php?type=zip&dir=subdir
// unzip deflate way zipped zip file
// zip.php?type=unzip&name=deflate
$type = $_GET['type'];
$type or exit('die no type to do!');
$name = isset($_GET['name']) ? $_GET['name'].'.zip' : 'zip.zip';
$subdir = $_GET['dir'];
$subdir or die('no sub dir to zip');
set_time_limit(0);
if ($type=='zip') {
$zip = new ZipArchive();
if ($zip->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;
}
//该片段来自于http://outofmemory.cn
精彩图集
精彩文章






