以前经常想虚拟主机上传和下载自己的网站。文件使用FTP命令一条一条执行传效率还是很低的。
我做了一个网站文件和XML文件互相转换的类,发布出来方便大家使用。
之所以使用XML而不是ZIP是有原因的:1、XML文件方便其他程序调用,特别是全能空间;2、使用ASP打包ZIP我还不会。
过几天我再发布一个ASP版的WEB2XML的类。哪位朋友对asp.net、jsp等熟悉,可以帮忙一起实现这个全能的WEB2XML。
<?php
/**
 * @author shadu at foxmail dot com
 * @version 2008-11-1
 * 
 * 方法1:create_xml2web(String,String)
 * 方法2:create_web2xml(String,String)
 * 
 * 打包网站指定目录到XML文件
 * Sample1:create_xml2web('/','web2xml.xml')
 * 
 * 释放XML文件到网站指定目录
 * Sample2:create_web2xml('/tmp/xx','web2xml.xml')
 * 
 * 1/使用该类中的方法可以将整个网站或某一个目录打包为XML文件,或从XML文件还原到网站
 * 2/存储XML文件时,对文件和文件内容采取unpack("H*",String)加密,避免了乱码现象
 * 3/空文件夹在打包时会丢弃
 * 4/程序不支持PHP命令行访问,必须通过Web访问。
 */
class webxml{
public $date = NULL; //file date
public $author = NULL; //file author
public $site = NULL; //site name
public $path = NULL; //path to pack
public $xmlfile = NULL; //xmlfile's name
public $filearray = NULL; //all the filenames with directory
public $count = NULL; //files count

//设置目标或源文件夹的目录和xml文件名,其中目录形式为:'/','/free/bbs',XML文件不要带路径
public function __construct($path=NULL,$xmlfile=NULL){
date_default_timezone_set('Asia/ShangHai');
$this->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'].$path;
}
if(NULL == $xmlfile){
$this->xmlfile = 'web2xml.xml' ;
}
else {
$this->xmlfile = $xmlfile;
}
if(NULL == $author){
$this->author = 'msn:shadu at foxmail dot com' ;
}
else {
$this->author = $author;
}
$this->site = $_SERVER['HTTP_HOST'];
}

/**
  * 返回文件列表
  * 使用参数$method = 1 返回带路径的文件名
  * 使用参数$method = 0 返回不带路径的文件名
  * 
  * @author [email protected]
  * @version 2008-10-26
  * @param String $dir
  * @param Int    $method
  * @return array/NULL
  */
public function array_fk_getfilelist($dir,$method = 0)
{
$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 = $this->array_fk_getfilelist($dir."/".$file,$method);
foreach ($childFile as $value) {
$fileArray[] = $value;
}
}
else
{
if(0 == $method)
$fileArray[]    =   $file;
else
$fileArray[] = $dir."/".$file;
}
}
}
return $fileArray;
}
else
{
//echo "File Handle Error!";
return NULL;
}
}

/**
  * 创建多层文件夹
  * @author www.php.net
  *
  * @param Str $pathname
  * @param Int $mode
  * @return unknown
  */
public function mkdir_recursive($pathname, $mode = '0777')
{
is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
}
/**
 * Create the XML file
 *
 */
public function create_web2xml(){
$this->filearray = $this->array_fk_getfilelist($this->path,1);
if(NULL==$this->filearray){
return 0;
}
$xmldoc = new DOMDocument();
$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
    $site = $xmldoc->createElement( "site" );
    $site->appendChild($xmldoc->createTextNode($this->site));
    $config->appendChild($site);
    $root->appendChild($config);
   
    $this->count = 0;
    //create the files element of root
    $files = $xmldoc->createElement("files");
    foreach ($this->filearray as $value) {
    //create file element    //except current php file
    if($value == $_SERVER['SCRIPT_FILENAME']){
    continue;
    }
    //except current xml file
    if($value == dirname($_SERVER['SCRIPT_FILENAME']).'/'.$this->xmlfile){
    continue;
    }
    //if(str_repeat()
    $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");
   
    //encode the file name to xml<file name="code...."></file>
    $filenametmp = unpack("H*",str_replace($this->path.'/',"",$value));
    $filename->appendChild($xmldoc->createTextNode($filenametmp[1]));
    $file->appendChild($filename);
    //add file to files
    $files->appendChild($file);
    $this->count++;
    }
    $root->appendChild($files);
    //save xml file
$xmldoc->save($this->xmlfile);
return $this->count;
}

/**
 * release file from xml to website
 *
 */
public function create_xml2web(){
$xmldoc = new DOMDocument();
$xmldoc->load($this->xmlfile);
$files = array();
$xmlfiles = $xmldoc->getElementsByTagName("files")->item(0);
foreach ($xmlfiles->getElementsByTagName("file") as $file) {
$filename = $file->getAttribute("name");
$files["$filename"] = $file->firstChild->nodeValue;
}
$this->count = 0;
foreach ($files as $filename => $file) {

//decode the file name form xml<file name="code...."></file>
$namedecode =  $this->path.'/'.pack("H*",$filename);

if(!is_dir(dirname($namedecode)))
{
$this->mkdir_recursive(dirname($namedecode));
}
file_put_contents($namedecode,pack("H*",$file));
$this->count++;
}
return $this->count;
}
}//$a = new webxml('/tmp');
//$a->create_web2xml();
//echo $a->count;
//
//$b = new webxml('/tmp/xx');
//$b->create_xml2web();
//echo $b->count;
//
?>

解决方案 »

  1.   

    XML文件示例<?xml version="1.0"?>
    <xmlweb>
      <config>
        <author>msn:shadu at foxmail dot com</author>
        <date>2008-10-31 20:10:56</date>
        <site>127.0.0.1</site>
      </config>
      <files>
        <file name="d6d0b9fa2fcae9c3fbcafd6162632e747874">7366617364660d0ad6d0b9fa</file>
        <file name="cae9c3fbcafd2e747874">7366617364660d0ae4b8ade59bbd</file>
      </files>
    </xmlweb>
      

  2.   

    有生成xlm的网站地图的代码吗?
      

  3.   

    实现站点sitemap防范至少有3种:
    1、没有网站权限,只能用程序把所有页面爬行一遍,然后生成。
    2、在网站上运行程序,生成地图
    3、有数据库结构,根据内部关系也能生成网站地图,如KingCMS内置的地图生成
      

  4.   

    收藏地址如下:
    http://www.winu.cn/space.php?uid=14160&do=blog&id=188
      

  5.   

    PHP实现网站打包为XML的类(加强版) 修正了一个Bug:空文件夹不能打包。
    增加了一个功能:打包前文件过滤。
    增加了扩展字段:md5校验值、文件大小、文件夹数目、文件数目等。
    支持中文文件夹。<?php
    /**
     * @author shadu####foxmail.com
     * @version v0.2 
     * @updated 2009-05-20
     * @desc Covert the files on the website to one XML,where excluede the special file extend name
     * @example $my_web2xml = new C_Web2XML('/bbs','bbs.xml');$my_web2xml->Set_FileExtendFiler('exe|rar|zip');$my_web2xml->Create_Web2XML();
     * @desc Covert the XML file to you website
     * @example $my_web2xml = new C_Web2XML('/bbs','bbs.xml');$my_web2xml->Create_XML2Web();
     */class C_Web2XML{
    private $date = NULL; //file date
    private $folderarray = array(); //all the folders with directory
    private $filearray = array(); //all the files with directory
    private $folders_count = 0;        //How many folders
    private $files_count = 0;        //how many files
    public  $author = "shadu###foxmail.com"; //file author
    public  $site_name = ""; //site name
    public  $path = ""; //path to pack
    public  $xmlfile = "web2xml.xml"; //xmlfile's name
    public  $filefilers = array();  //the files you can skip

    /**
     * Init the parameters: path and the xml file name
     *
     * @param string $path
     * @param string $xmlfile
     */
    public function __construct($path=NULL, $xmlfile=NULL){
    date_default_timezone_set('Asia/ShangHai');
    $this->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;
    }
    }
      

  6.   


    /**
     * 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();?>xml文件示例
    <?xml version="1.0" encoding="UTF-8"?>
    <xmlweb>
    <config>
    <author>shadu###foxmail.com</author>
    <date>2009-05-22 01:05:06</date>
    <sitename>127.0.0.1</sitename>
    </config>
    <folders total="1">
    <folder>73696e61</folder>
    </folders>
    <files total="3">
    <file name="612e747874" md5="90bc31a87b2c14fac1c22b18bc2df790" size="6">736664736666</file>
    </files>
    </xmlweb>