PHP Site Backup program code sharing _php Tips

Source: Internet
Author: User
Tags bit set crc32 explode php and pow time and date

Effect Chart:

PHP code

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Website Program Backup </title>
<body>
<form name= "MyForm" method= "POST" action= "" >
<?php
Error_reporting (E_all & ~e_notice);
Ini_set (' Memory_limit ', ' 2048M ');
echo "Select the file or directory:<br> to compress";
$fdir = Opendir ('./');
while ($file =readdir ($fdir))
{
if ($file = = '. ' | | $file = = ' ... ')
Continue
echo "<input name= ' dfile[] ' type= ' checkbox ' value= ' $file '". ($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>
Contains the following file types:
<input name= "File_type" type= "text" id= "File_type" "value=" "size=" ">
<font color= "Red" >
(File type with "|" Separated, default null contains arbitrary files, for example: if you need to package PHP and JPG files, enter "Php|jpg"
</font>
<br>
To save a compressed file to a directory:
<input name= "Todir" type= "text" id= "Todir" value= "__dwb2011__" size= ">"
<font color= "Red" >
(Leave blank for this directory, must have write permission)
</font>
<br>
Compressed file name:
<input name= "Zipname" type= "text" id= "Zipname" value= "Dwb2011.zip" size= ">"
<font color= "Red" >
(. zip)
</font>
<br>
<br>
<input name= "myaction" type= "hidden" id= "myaction" value= "Dozip" >
<input type= ' button ' value= ' reverse ' onclick= ' selrev (); ' >
<input type= "Submit" name= "Submit" value= "start Compression" >
<script language= ' JavaScript ' >
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 = '; This variable is accessed only by the class external program
var $gzfilename;
var $fp;
var $dirstr = ';
var $filefilters = array ();
function Setfilefilter ($filetype)
{
$this->filefilters = explode (' | ', $filetype);
}
Returns the modified time format for the file.
Called only for internal functions in 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,
and returns write permission for the file.
function Startfile ($path = ' dodo.zip ')
{
$this->gzfilename= $path;
$mypathdir =array ();
Todo
{
$mypathdir [] = $path = DirName ($path);
while ($path!= '. ');
@end ($mypathdir);
Todo
{
$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 compression package.
function AddFile ($data, $name)
{
$name = str_replace (' \ \ ', '/', $name);
if (STRRCHR ($name, '/') = = '/')
return $this->adddir ($name);
if (!empty ($this->filefilters))
{
if (!in_array (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);
New Add File content formatting:
$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 (' 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); Write new file contents
$my _datastr_len = strlen ($DATASTR);
Unset ($DATASTR);
New Add File directory information
$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 (' 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; Directory information
$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); Write new file contents
$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"), pack ("V", $this->datastr_len). $name;
$this->dirstr. = $dirstr; Directory information
$this-> File_count + +;
$this-> Dirstr_len + + strlen ($DIRSTR);
$this-> Datastr_len + + $my _datastr_len;
}
function CreateFile ()
{
Compressed package end information, including total number of files, directory information read pointer location, and other information
$endstr = "\x50\x4b\x05\x06\x00\x00\x00\x00".
Pack (' V ', $this-> file_count).
Pack (' V ', $this-> file_count).
Pack (' V ', $this-> dirstr_len).
Pack (' V ', $this-> datastr_len).
"\x00\x00";
Fwrite ($this->fp, $this->dirstr. $endstr);
Fclose ($this-&GT;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 <count ($bitunit); $key + +)
{
if ($num >=pow (2,10* $key)-1)
{//1023b will appear as 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 "is adding compressed file ...<br><br>";
$filenum = 0;
foreach ($_request[dfile] as $file)
{
if (Is_file ($file))
{
if (!empty ($dodozip-> filefilters))
if (!in_array (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> compression complete, add $filenum files altogether. <br><a href= ' $_request[todir]$_request[zipname] ' _fcksavedurl= ' $_ Request[todir]$_request[zipname] ' >$_request[todir]$_request[zipname ' (". Num_bitunit" (FileSize ("$_REQUEST[ Todir]$_request[zipname] ").") </a> ";
}
Else
{
echo "$_request[todir]$_request[zipname] cannot be written, please check that the path or permissions are correct .<br>";
}
}
?>
</form>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.