PHP website backup program code sharing

Source: Internet
Author: User
Tags bit set crc32 php website

:

PHP code
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> website program backup </title>
</Head>
<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 to be compressed: <br> ";
$ 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 = "50">
<Font color = "red">
(The file type is separated by "|". If it is null by default, it contains any file. For example, if you want to package a php or jpg file, enter "php | jpg ")
</Font>
<Br>
Save the compressed file to the directory:
<Input name = "todir" type = "text" id = "todir" value = "_ dwb2011 _" size = "15">
<Font color = "red">
(Leave this directory empty and you must have the write permission)
</Font>
<Br>
Compressed file name:
<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 = 'button 'value = 'select '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 only accessed by external programs.
Var $ gzfilename;
Var $ fp;
Var $ dirstr = '';
Var $ filefilters = array ();
Function SetFileFilter ($ filetype)
{
$ This-> filefilters = explode ('|', $ filetype );
}
// Return the modified time format of the object.
// Calls only 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 ['urs'] = 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 and create a file directory,
// And return the write permission for the object.
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 ($ zdata, 0, strlen ($ zdata)-4), 2 );
// Format the content of the newly added file:
$ 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 content
$ My_datastr_len = strlen ($ datastr );
Unset ($ datastr );
// Add new file directory information
$ Dirstr = "\ x50 \ x4b \ x01 \ x02 ";
$ Dirstr. = "\ x00 \ x00"; // version made
$ 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 content
$ 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; // directory information
$ This-> file_count ++;
$ This-> dirstr_len + = strlen ($ dirstr );
$ This-> datastr_len + = $ my_datastr_len;
}
Function createfile ()
{
// End information of the compressed package, including the total number of files, directory information reading 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-> fp );
}
}
If (! Trim ($ _ REQUEST [zipname])
$ _ REQUEST [zipname] = "dodozip.zip ";
Else
$ _ REQUEST [zipname] = trim ($ _ REQUEST [zipname]);
If (! Strrchr(strtolower(pai_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 is 1 KB
$ 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 "adding compressed file... <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> compressed. A total of $ filenum files are added. <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. Check whether the path or permission is correct. <br> ";
}
}
?>
</Form>
</Body>
</Html>

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.