PHP website backup program code sharing_PHP tutorial

WBOY
Release: 2016-07-21 15:28:43
Original
1014 people have browsed it

Rendering:
PHP website backup program code sharing_PHP tutorial
PHP code

Copy code The code is as follows:




< title>Website program backup



error_reporting(E_ALL & ~E_NOTICE);
ini_set('memory_limit', '2048M');
echo "Select a file or directory to compress:
";
$fdir = opendir('./');
while($file=readdir($fdir))
{
if($file=='.'|| $file=='. .')
continue;
echo " ";
if(is_file($file))
{
echo "2 $ file
";
}
else
{
echo "0 $file
";
}
}
?>


Contains the following file types:


(File types are separated by "|", the default is empty to include any file, for example: if you need to package php and jpg files, enter "php|jpg")



Save the compressed file to the directory:


(leave blank for this directory, must have write permission)



Compressed file name:


(.zip)








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 = ''; //This variable is only accessed by programs outside the class
var $gzfilename;
var $fp;
var $dirstr='';
var $filefilters = array();
function SetFileFilter($filetype)
{
$this->filefilters = explode('|',$filetype);
}
//Return the modification time format of the file.
//Only called for internal functions of this class.
function unix2DosTime($unixtime = 0)
{
$timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
if ($timearray['year'] < 1980)
{
$timearray['year'] = 1980;
$timearray[ 'mon'] = 1;
$timearray['mday'] = 1;
$timearray['hours'] = 0;
$timearray['minutes'] = 0;
$ timearray['seconds'] = 0;
}
return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray[ 'seconds'] >> 1);
}
//Initialize the file, create the file directory,
//Return the write permission of the file.
function startfile($path = ' dodo.zip')
{
$this->gzfilename=$path;
$mypathdir=array();
do
{
$mypathdir[] = $path = dirname($path);
} while($path != '.');
@end($mypathdir);
do
{
$path = @current($ mypathdir);
@mkdir($path);
} while(@prev($mypathdir));
if($this->fp=@fopen($this->gzfilename," w"))
{
return true;
}
return false;
}
//Add a file to the zip package.
function addfile($data , $name)
{
$name = str_replace('\', '/', $name);
if(strrchr($name,'/')=='/')
return $this->adddir($name);
if(!empty($this->filefilters))
{
if (!in_array(end(explode(".",$ name)), $this->filefilters))
{
return;
}
}
$dtime = dechex($this->unix2DosTime());
$hexdtime = 'x' . $dtime[6] . $dtime[7] . 'x' . $dtime[4] . $dtime[5] . 'x' . $dtime[2] . $dtime[3] . 'x' . $dtime[0] . $dtime[1];
eval('$hexdtime = "' . $hexdtime . '";');
$unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data);
$c_len = strlen($zdata);
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
//新添文件内容格式化:
$datastr = "x50x4bx03x04";
$datastr .= "x14x00"; // ver needed to extract
$datastr .= "x00x00"; // gen purpose bit flag
$datastr .= "x08x00"; // compression method
$datastr .= $hexdtime; // last mod time and date
$datastr .= pack('V', $crc); // crc32
$datastr .= pack('V', $c_len); // compressed filesize
$datastr .= pack('V', $unc_len); // uncompressed filesize
$datastr .= pack('v', strlen($name)); // length of filename
$datastr .= pack('v', 0); // extra field length
$datastr .= $name;
$datastr .= $zdata;
$datastr .= pack('V', $crc); // crc32
$datastr .= pack('V', $c_len); // compressed filesize
$datastr .= pack('V', $unc_len); // uncompressed filesize
fwrite($this->fp,$datastr); //写入新的文件内容
$my_datastr_len = strlen($datastr);
unset($datastr);
//新添文件目录信息
$dirstr = "x50x4bx01x02";
$dirstr .= "x00x00"; // version made by
$dirstr .= "x14x00"; // version needed to extract
$dirstr .= "x00x00"; // gen purpose bit flag
$dirstr .= "x08x00"; // compression method
$dirstr .= $hexdtime; // last mod time & date
$dirstr .= pack('V', $crc); // crc32
$dirstr .= pack('V', $c_len); // compressed filesize
$dirstr .= pack('V', $unc_len); // uncompressed filesize
$dirstr .= pack('v', strlen($name) ); // length of filename
$dirstr .= pack('v', 0 ); // extra field length
$dirstr .= pack('v', 0 ); // file comment length
$dirstr .= pack('v', 0 ); // disk number start
$dirstr .= pack('v', 0 ); // internal file attributes
$dirstr .= pack('V', 32 ); // external file attributes - 'archive' bit set
$dirstr .= pack('V',$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 = "x50x4bx03x04x0ax00x00x00x00x00x00x00x00x00";
$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 = "x50x4bx01x02x00x00x0ax00x00x00x00x00x00x00x00x00";
$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 = "x50x4bx05x06x00x00x00x00" .
pack('v', $this -> file_count) .
pack('v', $this -> file_count) .
pack('V', $this -> dirstr_len) .
pack('V', $this -> datastr_len) .
"x00x00";
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]),'.')=='.zip')
$_REQUEST[zipname] .= ".zip";
$_REQUEST[todir] = str_replace('\','/',trim($_REQUEST[todir]));
if(!strrchr(strtolower($_REQUEST[todir]),'/')=='/')
$_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('',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('',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(' B',' KB',' MB',' GB');
for($key=0;$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 "正在添加压缩文件...

";
$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 "2  $file
";
}
else
{
echo "0 $file
";
}
$filenum += listfiles($file);
}
$dodozip -> createfile();
echo "
压缩完成,共添加 $filenum 个文件.
$_REQUEST[todir]$_REQUEST[zipname] (".num_bitunit(filesize("$_REQUEST[todir]$_REQUEST[zipname]")).")";
}
else
{
echo "$_REQUEST[todir]$_REQUEST[zipname] 不能写入,请检查路径或权限是否正确.
";
}
}
?>



www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323545.htmlTechArticle效果图: PHP代码 复制代码 代码如下: html head meta http-equiv="Content-Type" content="text/html; charset=gb2312" title网站程序备份/title /head body form name="myf...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template