Home  >  Article  >  php教程  >  PHP网站备份程序

PHP网站备份程序

PHP中文网
PHP中文网Original
2016-05-25 17:14:031418browse

php代码

<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
<title>网站程序备份</title>
</head>  
<body>  
<form name="myform" method="post" action="">
<?php
    error_reporting(E_ALL & ~E_NOTICE);  
	ini_set(&#39;memory_limit&#39;, &#39;2048M&#39;);
	echo "选择要压缩的文件或目录:<br>";  
	$fdir = opendir(&#39;./&#39;);  
	while($file=readdir($fdir))
	{  
		if($file==&#39;.&#39;|| $file==&#39;..&#39;) 
		continue;  
		echo "<input name=&#39;dfile[]&#39; type=&#39;checkbox&#39; value=&#39;$file&#39; ".($file==basename(__FILE__)?"":"checked")."> ";  
	
		if(is_file($file))
		{  
			echo "<font face=\"wingdings\" size=\"5\">2</font>  $file<br>";  
		}
		else
		{  
			echo "<font face=\"wingdings\" size=\"5\">0</font> $file<br>";  
		}  
	}  
?>  
<br>
包含下列文件类型:
<input name="file_type" type="text" id="file_type" value="" size="50">  
<font color="red">
(文件类型用"|"隔开,默认空则包含任意文件,例:如果需要打包php和jpg文件,则输入"php|jpg")
</font>
<br>
压缩文件保存到目录:  
<input name="todir" type="text" id="todir" value="__dwb2011__" size="15">  
<font color="red">
(留空为本目录,必须有写入权限)
</font>
<br>  
压缩文件名称:  
<input name="zipname" type="text" id="zipname" value="dwb2011.zip" size="15">  
<font color="red">
(.zip)
</font>
<br>  
<br>
<input name="myaction" type="hidden" id="myaction" value="dozip">  
<input type=&#39;button&#39; value=&#39;反选&#39; onclick=&#39;selrev();&#39;>  
<input type="submit" name="Submit" value=" 开始压缩 ">  
<script language=&#39;javascript&#39;>  
	function selrev() 
	{  
		with(document.myform) 
		{  
			for(i=0;i<elements.length;i++) 
			{  
				thiselm = elements[i];  
				if(thiselm.name.match(/dfile\[]/)) 
					thiselm.checked = !thiselm.checked;  
			}  
		}  
	}  
</script>  

<?php
error_reporting(E_ALL & ~E_NOTICE);
set_time_limit(0);  
class PHPzip
{  
	var $file_count = 0 ;  
	var $datastr_len   = 0;  
	var $dirstr_len = 0;  
	var $filedata = &#39;&#39;; //该变量只被类外部程序访问  
	var $gzfilename;  
	var $fp;  
	var $dirstr=&#39;&#39;;  
	var $filefilters = array();

	function SetFileFilter($filetype)
	{
        $this->filefilters = explode(&#39;|&#39;,$filetype);
    }
	
	//返回文件的修改时间格式.  
	//只为本类内部函数调用.  
	function unix2DosTime($unixtime = 0) 
	{  
		$timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);  
		if ($timearray[&#39;year&#39;] < 1980) 
		{  
			$timearray[&#39;year&#39;]    = 1980;  
			$timearray[&#39;mon&#39;]     = 1;  
			$timearray[&#39;mday&#39;]    = 1;  
			$timearray[&#39;hours&#39;]   = 0;  
			$timearray[&#39;minutes&#39;] = 0;  
			$timearray[&#39;seconds&#39;] = 0;  
		}  
		return (($timearray[&#39;year&#39;] - 1980) << 25) | ($timearray[&#39;mon&#39;] << 21) | ($timearray[&#39;mday&#39;] << 16) |  ($timearray[&#39;hours&#39;] << 11) | ($timearray[&#39;minutes&#39;] << 5) | ($timearray[&#39;seconds&#39;] >> 1);  
	}  

	//初始化文件,建立文件目录,  
	//并返回文件的写入权限.  
	function startfile($path = &#39;dodo.zip&#39;)
	{  
		$this->gzfilename=$path;  
		$mypathdir=array();  

		do
		{  
			$mypathdir[] = $path = dirname($path);  
		}	while($path != &#39;.&#39;);  

		@end($mypathdir);  

		do
		{  
			$path = @current($mypathdir);  
			@mkdir($path);  
		}	while(@prev($mypathdir));  

		if($this->fp=@fopen($this->gzfilename,"w"))
		{  
			return true;  
		}  

		return false;  
	}  

	//添加一个文件到 zip 压缩包中.  
	function addfile($data, $name)
	{  
		$name = str_replace(&#39;\\&#39;, &#39;/&#39;, $name); 
		if(strrchr($name,&#39;/&#39;)==&#39;/&#39;) 
			return $this->adddir($name);
		if(!empty($this->filefilters))
		{
			if (!in_array(end(explode(".",$name)), $this->filefilters))
			{
				return;
			}
		}
		$dtime = dechex($this->unix2DosTime()); 
		$hexdtime = &#39;\x&#39; . $dtime[6] . $dtime[7] . &#39;\x&#39; . $dtime[4] . $dtime[5] . &#39;\x&#39; . $dtime[2] . $dtime[3] . &#39;\x&#39; . $dtime[0] . $dtime[1]; 
		eval(&#39;$hexdtime = "&#39; . $hexdtime . &#39;";&#39;); 

		$unc_len = strlen($data); 
		$crc	 = crc32($data); 
		$zdata   = gzcompress($data); 
		$c_len   = strlen($zdata); 
		$zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); 

		//新添文件内容格式化: 
		$datastr  = "\x50\x4b\x03\x04"; 
		$datastr .= "\x14\x00";            // ver needed to extract 
		$datastr .= "\x00\x00";            // gen purpose bit flag 
		$datastr .= "\x08\x00";            // compression method 
		$datastr .= $hexdtime;             // last mod time and date 
		$datastr .= pack(&#39;V&#39;, $crc);             // crc32 
		$datastr .= pack(&#39;V&#39;, $c_len);           // compressed filesize 
		$datastr .= pack(&#39;V&#39;, $unc_len);         // uncompressed filesize 
		$datastr .= pack(&#39;v&#39;, strlen($name));    // length of filename 
		$datastr .= pack(&#39;v&#39;, 0);                // extra field length 
		$datastr .= $name; 
		$datastr .= $zdata; 
		$datastr .= pack(&#39;V&#39;, $crc);                 // crc32 
		$datastr .= pack(&#39;V&#39;, $c_len);               // compressed filesize 
		$datastr .= pack(&#39;V&#39;, $unc_len);             // uncompressed filesize 
		fwrite($this->fp,$datastr); //写入新的文件内容 
		$my_datastr_len = strlen($datastr); 
		unset($datastr); 

		//新添文件目录信息 
		$dirstr  = "\x50\x4b\x01\x02"; 
		$dirstr .= "\x00\x00";                 // version made by 
		$dirstr .= "\x14\x00";                 // version needed to extract 
		$dirstr .= "\x00\x00";                 // gen purpose bit flag 
		$dirstr .= "\x08\x00";                 // compression method 
		$dirstr .= $hexdtime;                  // last mod time & date 
		$dirstr .= pack(&#39;V&#39;, $crc);            // crc32 
		$dirstr .= pack(&#39;V&#39;, $c_len);          // compressed filesize 
		$dirstr .= pack(&#39;V&#39;, $unc_len);        // uncompressed filesize 
		$dirstr .= pack(&#39;v&#39;, strlen($name) );  // length of filename 
		$dirstr .= pack(&#39;v&#39;, 0 );              // extra field length 
		$dirstr .= pack(&#39;v&#39;, 0 );              // file comment length 
		$dirstr .= pack(&#39;v&#39;, 0 );              // disk number start 
		$dirstr .= pack(&#39;v&#39;, 0 );              // internal file attributes 
		$dirstr .= pack(&#39;V&#39;, 32 );             // external file attributes - &#39;archive&#39; bit set 
		$dirstr .= pack(&#39;V&#39;,$this->datastr_len ); // relative offset of local header 
		$dirstr .= $name; 
		$this->dirstr .= $dirstr; //目录信息 
		$this -> file_count ++; 
		$this -> dirstr_len += strlen($dirstr); 
		$this -> datastr_len += $my_datastr_len; 
	} 

	function adddir($name)
	{ 
		$name = str_replace("\\", "/", $name); 
		$datastr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"; 
		$datastr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) ); 
		$datastr .= pack("v", 0 ).$name.pack("V", 0).pack("V", 0).pack("V", 0); 
		fwrite($this->fp,$datastr); //写入新的文件内容 
		$my_datastr_len = strlen($datastr); 
		unset($datastr); 
		$dirstr = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"; 
		$dirstr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) ); 
		$dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 ); 
		$dirstr .= pack("V", 16 ).pack("V",$this->datastr_len).$name; 
		$this->dirstr .= $dirstr; //目录信息 
		$this -> file_count ++; 
		$this -> dirstr_len += strlen($dirstr); 
		$this -> datastr_len += $my_datastr_len; 
	} 

	function createfile()
	{ 
		//压缩包结束信息,包括文件总数,目录信息读取指针位置等信息 
		$endstr = "\x50\x4b\x05\x06\x00\x00\x00\x00" . 
		pack(&#39;v&#39;, $this -> file_count) . 
		pack(&#39;v&#39;, $this -> file_count) . 
		pack(&#39;V&#39;, $this -> dirstr_len) . 
		pack(&#39;V&#39;, $this -> datastr_len) . 
		"\x00\x00"; 
		fwrite($this->fp,$this->dirstr.$endstr); 
		fclose($this->fp); 
	} 
} 

if(!trim($_REQUEST[zipname])) 
	$_REQUEST[zipname] = "dodozip.zip"; 
else 
	$_REQUEST[zipname] = trim($_REQUEST[zipname]); 

if(!strrchr(strtolower($_REQUEST[zipname]),&#39;.&#39;)==&#39;.zip&#39;) 
	$_REQUEST[zipname] .= ".zip"; 

$_REQUEST[todir] = str_replace(&#39;\\&#39;,&#39;/&#39;,trim($_REQUEST[todir])); 

if(!strrchr(strtolower($_REQUEST[todir]),&#39;/&#39;)==&#39;/&#39;) 
	$_REQUEST[todir] .= "/"; 

if($_REQUEST[todir]=="/") 
	$_REQUEST[todir] = "./"; 

function listfiles($dir=".")
{ 
	global $dodozip; 
	$sub_file_num = 0; 
	if(is_file("$dir"))
	{ 
		if(realpath($dodozip ->gzfilename)!=realpath("$dir"))
		{ 
			$dodozip -> addfile(implode(&#39;&#39;,file("$dir")),"$dir"); 
			return 1; 
		} 
		return 0; 
	} 

	$handle=opendir("$dir"); 
	while ($file = readdir($handle)) 
	{ 
		if($file=="."||$file=="..")
			continue; 

		if(is_dir("$dir/$file"))
		{ 
			$sub_file_num += listfiles("$dir/$file"); 
		} 
		else 
		{ 
			if(realpath($dodozip ->gzfilename)!=realpath("$dir/$file"))
			{ 
				$dodozip -> addfile(implode(&#39;&#39;,file("$dir/$file")),"$dir/$file"); 
				$sub_file_num ++; 
			} 
		} 
	} 

	closedir($handle); 

	if(!$sub_file_num) 
		$dodozip -> addfile("","$dir/"); 

	return $sub_file_num; 
} 

function num_bitunit($num)
{ 
	$bitunit=array(&#39; B&#39;,&#39; KB&#39;,&#39; MB&#39;,&#39; GB&#39;); 
	for($key=0;$key<count($bitunit);$key++)
	{ 
		if($num>=pow(2,10*$key)-1)
		{ //1023B 会显示为 1KB 
			$num_bitunit_str=(ceil($num/pow(2,10*$key)*100)/100)." $bitunit[$key]"; 
		} 
	} 
	return $num_bitunit_str; 
} 

if(is_array($_REQUEST[dfile]))
{ 
	$dodozip = new PHPzip; 
	if($_REQUEST["file_type"] != NULL)
		$dodozip -> SetFileFilter($_REQUEST["file_type"]);
	if($dodozip -> startfile("$_REQUEST[todir]$_REQUEST[zipname]"))
	{ 
		echo "正在添加压缩文件...<br><br>"; 
		$filenum = 0; 
		foreach($_REQUEST[dfile] as $file)
		{ 
			if(is_file($file))
			{ 
				if(!empty($dodozip -> filefilters))
					if (!in_array(end(explode(".",$file)), $dodozip -> filefilters))
						continue;
				echo "<font face=\"wingdings\" size=\"5\">2</font>  $file<br>"; 
			}
			else
			{ 
				echo "<font face=\"wingdings\" size=\"5\">0</font> $file<br>"; 
			} 
			$filenum += listfiles($file); 
		} 
		$dodozip -> createfile(); 

		echo "<br>压缩完成,共添加 $filenum 个文件.<br><a href=&#39;$_REQUEST[todir]$_REQUEST[zipname]&#39; _fcksavedurl=&#39;$_REQUEST[todir]$_REQUEST[zipname]&#39;>$_REQUEST[todir]$_REQUEST[zipname] (".num_bitunit(filesize("$_REQUEST[todir]$_REQUEST[zipname]")).")</a>"; 
	}
	else
	{ 
		echo "$_REQUEST[todir]$_REQUEST[zipname] 不能写入,请检查路径或权限是否正确.<br>"; 
	} 
}
?>  
</form>
</body>  
</html>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn