date = date("Y-m-d H:m:s"); if(NULL == $path || '/' == $path){ //d:/inetpub/wwwroot $this->path = $_SERVER["DOCUMENT_ROOT"]; } else { //d:/inetpub/wwwroot/bbs $this->path = $_SERVER["DOCUMENT_ROOT"].Patch_Format($path); } if(NULL != $xmlfile){ $this->xmlfile = $xmlfile; ; } $this->site_name = $_SERVER["SERVER_NAME"]; } /** * If the file's extend name is in array ,don't put it in xml * * @param string $filetype */ public function Set_FileExtendFiler($filetype){ $this->filefilers = explode('|',$filetype); } /** * Returns the number of bytes written or FALSE if an error occurred. * * @return unknown */ public function Create_Web2XML(){ $this->filearray = Array_Get_FileList($this->path); $this->files_count = count($this->filearray); $this->folderarray = Array_Get_FolderList($this->path); $this->folders_count = count($this->folderarray); $xmldoc = new DOMDocument('1.0','UTF-8'); $root = $xmldoc->createElement("xmlweb"); $xmldoc->appendChild($root); //create the config element of root $config = $xmldoc->createElement("config"); //add the author of xmlfile $author = $xmldoc->createElement( "author" ); $author->appendChild($xmldoc->createTextNode($this->author)); $config->appendChild($author); //add the date of xmlfile $date = $xmldoc->createElement( "date" ); $date->appendChild($xmldoc->createTextNode($this->date)); $config->appendChild($date); //add the name of site $sitename = $xmldoc->createElement( "sitename" ); $sitename->appendChild($xmldoc->createTextNode($this->site_name)); $config->appendChild($sitename); $root->appendChild($config); //create folders element $folders = $xmldoc->createElement("folders"); $folderstotal = $xmldoc->createAttribute("total"); $folderstotal->appendChild($xmldoc->createTextNode($this->folders_count)); $folders->appendChild($folderstotal); //create folder element foreach ($this->folderarray as $value) { $folder = $xmldoc->createElement("folder"); $folderEncode = unpack("H*",str_replace($this->path.'/',"",$value)); $folder->appendChild($xmldoc->createTextNode($folderEncode[1])); $folders->appendChild($folder); } $root->appendChild($folders); //create files element $files = $xmldoc->createElement("files"); //create file element $filestotal = $xmldoc->createAttribute("total"); $filestotal->appendChild($xmldoc->createTextNode($this->files_count)); $files->appendChild($filestotal); foreach ($this->filearray as $value) { //except current php file if($value == $_SERVER['SCRIPT_FILENAME']){ continue; } //except current xml file if($value == dirname($_SERVER['SCRIPT_FILENAME']).'/'.$this->xmlfile){ continue; } if (in_array(end(explode(".",$value)), $this->filefilers)){ continue; } $file = $xmldoc->createElement("file"); //add file content $filecontent = unpack("H*",file_get_contents($value)); $file->appendChild($xmldoc->createTextNode($filecontent[1])); //add file attribute name $filename = $xmldoc->createAttribute("name"); $filenameEncode = unpack("H*",str_replace($this->path.'/',"",$value)); $filename->appendChild($xmldoc->createTextNode($filenameEncode[1])); $file->appendChild($filename); $filemd5 = $xmldoc->createAttribute("md5"); $filemd5->appendChild($xmldoc->createTextNode(md5_file($value))); $file->appendChild($filemd5); $filesize = $xmldoc->createAttribute("size"); $filesize->appendChild($xmldoc->createTextNode(filesize($value))); $file->appendChild($filesize); //add file to files $files->appendChild($file); } $root->appendChild($files); return $xmldoc->save($this->xmlfile); } /** * free the files from xml file to target path * return the total number of files * @return int */ public function Create_XML2Web(){ $xmldoc = new DOMDocument(); $xmldoc->load($this->xmlfile); //create folders $folders = array(); $xmlfolders = $xmldoc->getElementsByTagName("folders")->item(0); foreach ($xmlfolders->getElementsByTagName("folder") as $folder) { $folders[] = $folder->firstChild->nodeValue; } foreach ($folders as $value) { mkdir_recursive($this->path.'/'.pack("H*",$value)); } //create files $files = array(); $xmlfiles = $xmldoc->getElementsByTagName("files")->item(0); foreach ($xmlfiles->getElementsByTagName("file") as $file) { $filename = $file->getAttribute("name"); $files["$filename"] = @$file->firstChild->nodeValue; //some files may be empty } $count = 0; foreach ($files as $filename => $file) { file_put_contents($this->path.'/'.pack("H*",$filename),pack("H*",$file)); $count ++; } return $count; } } /** * Get folder list of the path * * @param string $path */ function Array_Get_FolderList($dir){ $folderArray = array(); $childFile = array(); if($handle = opendir($dir)) { while(($file = readdir($handle)) !== false) { if($file !="." && $file !="..") { if(is_dir($dir."/".$file)) { $folderArray[] = $dir."/".$file; //Go to ChildFolder $childFile = Array_Get_FolderList($dir."/".$file); foreach ($childFile as $value) { $folderArray[] = $value; } } } } return $folderArray; } else { //echo "File Handle Error!"; return NULL; } } /** * Get file list of the path * * @param string $dir */ function Array_Get_FileList($dir){ $fileArray = array(); $childFile = array(); if($handle = opendir($dir)) { while(($file = readdir($handle)) !== false) { if($file !="." && $file !="..") { if(is_dir($dir."/".$file)) { //Go to ChildFolder $childFile = Array_Get_FileList($dir."/".$file); foreach ($childFile as $value) { $fileArray[] = $value; } } else { $fileArray[] = $dir."/".$file; } } } return $fileArray; } else { //echo "File Handle Error!"; return NULL; } } /** * substr which support utf-8 * * @param string $str * @param int $start * @param int $len * @return string */ function msubstr($str, $start, $len) { $tmpstr = ""; $strlen = $start + $len; for($i = 0; $i < $strlen; $i++) { if(ord(substr($str, $i, 1)) > 0xa0) { $tmpstr .= substr($str, $i, 2); $i++; } else $tmpstr .= substr($str, $i, 1); } return $tmpstr; } /** * Make Folders in loop * @author www.php.net * * @param String $pathname * @param Interge $mode * @return unknown */ function mkdir_recursive($pathname, $mode = '0777') { is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname), $mode); return is_dir($pathname) || @mkdir($pathname, $mode); } /** * Format the string to startd webroot * * @param unknown_type $path * @return unknown */ function Patch_Format($path){ if ('/' != $path[0]) { $path = '/'.$path; } if ('/' == $path[strlen($path)-1]) { $path = substr($path, 0, strlen($path)-1); } return $path; } set_time_limit(0); //example /* $my_web2xml = new C_Web2XML('/bbs1','web2xml.xml'); $my_web2xml->Set_FileExtendFiler('rar|zip|exe'); //pack all the files exclude rar,zip,exe $my_web2xml->Create_Web2XML(); $my_xml2web = new C_Web2XML('/bbs2','web2xml.xml'); $my_xml2web->Create_XML2Web(); */ $do = $_POST["do"]; if($do == "web2xml") { $web2xml_path = $_POST["web2xml_path"]; $web2xml_type = $_POST["web2xml_type"]; if(isset($web2xml_path)) { $my_web2xml = new C_Web2XML($web2xml_path, $web2xml_path.'.xml'); if(isset($web2xml_type)) $my_web2xml->Set_FileExtendFiler($web2xml_type); $my_web2xml->Create_Web2XML(); } } else if($do == "xml2web") { $xml2web_path = $_POST["xml2web_path"]; if(isset($xml2web_path)) { $my_xml2web = new C_Web2XML($xml2web_path, $xml2web_path.'.xml'); $my_xml2web->Create_XML2Web(); } } ?> 网站整站压缩解压PHP版-By DoDo

此程序可以将整个网站或者某一个目录打包成XML文件,或从XML文件还原到网站







.xml 输入文件名,无需后缀,务必将xml文件与此php程序放在同一目录下

DoDo's Blog