其中几个函数很好用,我花了一天时间
直接创建多层目录mymkdir($dirname),
批处理目录下的文件function listDirTree( $dirName = null ,$callFunction) 可自定义处理函数。
如:

        $callFunc="backupFile(\"$backupPath\",\$file,\$filePath,\$dirName);";

        listDirTree($operateDir,$callFunc);


将文本中 x=y
                   u=a
提取为数组的函数
function myExplode($originalStr,$firstTag,$secondTag)

将文件转换为utf-8编码的函数。function gb2utf8($file,$filePath)
判断文件是否为utf-8编码的函数 function utf8_probability(&$rawtextstr)

 

<?php
//header('Content-Type: text/html; charset=utf-8');
$operateDir="d:/usr/www/html/mambog";        //需要备份的目录
$backupPath="d:/usr/www/html/bkup_mambog/"//需要备份到的目录
echo "<PRE>";
$currentname
="fileconv.php";
$currentpath
=realpath($currentname);
$pos 
= strrpos ($currentpath,"\\" );
$currentpath
=substr($currentpath,0,$pos);
$currentpath
=str_replace("\\","/",$currentpath);

echo 
"当前执行文件所在目录:$currentpath <BR>";
$tm1
=microtime();
$extname
=".php;.xml;html;.htm;.css;.txt;.js;.ini;";
$utf8File
=0;
$convertedFile
=0;
$notTextFile
=0;

$operateDirLen
=strlen($operateDir);
mainsub(
&$operateDir,&$backupPath);
function mainsub(&$operateDir,&$backupPath)
{
    
if(strpos($backupPath,$operateDir)===false)
    {    
        
if(!is_dir($backupPath))mymkdir($backupPath);
        backup(
&$operateDir,$backupPath);
        $callFunc
="gb2utf8(\$file,\$filePath);";
        listDirTree($operateDir,$callFunc);
    }
    
else
    {
        echo 
"Fetal Error You must change your backupdir ,<BR>It can't be under the directory you want to backup";
        die;
    }
}

function backup($operateDir,$backupPath)
{    
    global $currentpath;
    
    
if(readtag($backupPath)==0)//如果没有备份过,那么,执行备份
    {    
        $callFunc
="backupFile(\"$backupPath\",\$file,\$filePath,\$dirName);";

        listDirTree($operateDir,$callFunc);

        setTag($backupPath); 
//设置备份标记
    }
    
else
    {
//如果备份过了,那么,exit;
        echo "backuped already";die;
    }

}
//function backup end

function backupFile($backupPath,$file,$filePath,$dirName)
{    global $operateDirLen;
    $subdir 
= substr($dirName."/",$operateDirLen+1);
    mymkdir($backupPath.$subdir); 
//创建子目录
    $bkfile=$backupPath.$subdir.$file;
    
//echo "备份文件为: $bkfile 原始文件为:$filePath<BR>";
$fstr=file_get_contents($filePath);
$fhandle
=fopen($bkfile,"wb");
fwrite($fhandle,$fstr);
fclose($fhandle);
}

echo (microtime()
-$tm1)."秒<BR>";

echo 
"<BR>utf8文件:".$utf8File;
echo 
"<BR>被转换的文件总数:".$convertedFile;
echo 
"<BR>非文本文件数目:".$notTextFile;



function readtag($backupDir)
{
    
if(!is_dir($backupDir)){
        mymkdir($backupDir);
    }
    
if(!is_file($backupDir."tag.ini"))  //如果tag.ini不存在,则建立并初始化
    {    if(!$fp=fopen($backupDir."tag.ini","w")){
            echo 
"error while reading or creating the tag.ini ";
        }
        
else
        {
            $str
="backuped=0";
            
if(!fwrite($fp,$str)){echo "error while init the tag.ini ";}
            fclose($fp);
        }
    gb2utf8(
"tag.ini",$backupDir."tag.ini"); //将新建的tag.ini转为utf-8编码
    return 0;
    }    
    
else  //如果tag.ini已经存在,读取其中的配置
    {
    $backupTag
=file_get_contents($backupDir."tag.ini");    
    $tagTree
=myExplode($backupTag,"/\n/","=");
    
//print_r($tagTree); //tagtree存储所有tag.ini里的 x=y 的信息
    return $tagTree['backuped'];
    }
}

function setTag($backupDir)
{
    $tagini
=file_get_contents($backupDir."tag.ini");
    
    
if(!$fp=fopen($backupDir."tag.ini","w")){
        echo 
"error while reading or creating the tag.ini ";
    }
    
else
    {    
        $str
="backuped=1";
        $pattern
="/backuped=0/";
        
if(!strpos($tagini,"backuped=",0))
        {
            $tagini
=$str;
        }
        
else
        {
            $tagini
=preg_replace($pattern,$str,$tagini);        
        }

        
if(!fwrite($fp,$tagini))
        {
            echo 
"error while set the tag.ini ";
        }
        fclose($fp);
    }
}
function myExplode($originalStr,$firstTag,$secondTag)
{
        $firstArray
=preg_split($firstTag,$originalStr);
        foreach($firstArray as $first)
        {
            $secondArray
=explode($secondTag,$first);
            $tagTree[$secondArray[
0]]=$secondArray[1];
        }

        
return $tagTree;
}

function mymkdir($mainDir)
{
if(!is_dir($mainDir))
{
global $validFatherDir;
getFatherDir($mainDir);
$fullLen
=strlen($mainDir); //控制循环
//
echo "需要创建的目录".$mainDir."长度为 $fullLen <BR>";
//
echo "最高有效的父目录".$validFatherDir."<BR>";
$validDirLen=strlen($validFatherDir);

    
while(($i<10 )&&($fullLen>$validDirLen+1))
    {
        
//echo "父目录长度:$validDirLen<BR>";
        $pos=strpos($mainDir,"/",$validDirLen+1);
        
//echo "/所在位置:$pos<BR>";
        $dirname=substr($mainDir,$validDirLen,$pos-$validDirLen);
        
//echo "正在创建目录".$validFatherDir.$dirname."<BR>";
        
        $validFatherDir.
=$dirname;
        
if(!mkdir($validFatherDir))
            {
            
//echo "Fetal Error accoured while create directory";die;
            }
        $validDirLen
=strlen($validFatherDir);
        $i
++;
    }
}
}
//mymkdir function end

function getFatherDir($mainDir)
{    global $validFatherDir;
    $pos 
= strrpos($mainDir,"/");
        
if ($pos===false)
        {
            
// not found
            echo "Fetal Error While Get Valid Father Dir";die;
            
return false;
        }
        
else
        {
            $fatherDir
=substr($mainDir,0,$pos);
            
if(!is_dir($fatherDir))
            {    echo 
"<font color=red >$fatherDir is not exists<BR></font>";
                getFatherDir($fatherDir);
            }
            
else
            {
                $validFatherDir
= $fatherDir;
                
//mkdir($mainDir);echo "$mainDir is created<BR>";

            }
        }
        
}
/** 函数 listDirTree( $dirName = null )
* 功能 列出目录下所有文件及子目录
* 参数 $dirName 目录名称
* 返回 目录结构数组 false为失败
*/

function listDirTree( $dirName = null ,$callFunction) 
{
//global $tree;

if( empty( $dirName ) )
exit( 
"IBFileSystem: directory is empty." );

if( is_dir( $dirName ) )
{
if( $dh = opendir( $dirName ) )
{
//$tree = array();

while( ( $file = readdir( $dh ) ) !== false )
{
if( $file != "." && $file != ".." )
{
$filePath 
= $dirName . "/" . $file;

if( is_dir( $filePath ) )//为目录,递归
{
    
//$tree[$file] = listDirTree( $filePath );
    listDirTree( $filePath ,$callFunction);
}
else//为文件,进行处理
{    //echo "文件处理函数为 $callFunction <BR>";
    eval($callFunction);
    
//gb2utf8($file,$filePath);

//$tree[] = $file;
//文件处理结束
}
}
closedir( $dh );

}
else
{
exit( 
"IBFileSystem: can not open directory $dirName.");
}

//返回当前的$tree
//
return $tree;
}

else
{
exit( 
"IBFileSystem: $dirName is not a directory.");
}
}

function test($arga,$argb)
{
echo $arga.
"<BR>".$argb."<BR>";
}

function gb2utf8($file,$filePath)
{    global $extname,$utf8File,$convertedFile,$notTextFile;

    $fileext
=substr($file,-4,4);

    preg_match(
"/".$fileext."/i",$extname, $matches);

    
if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.由数组$matches定义
    {
    $outfilename
=$filePath;

    $fstr
=file_get_contents($outfilename);

    $utf8score
=utf8_probability(&$fstr);

        
if90<=$utf8score && $utf8score<=100)
            {    $utf8File
++;
                
//echo "<font color=red> $filePath  is encoded with utf-8 already </font><BR>";//die;
            }
        
else
            {
            $fp
=fopen($outfilename,'wb');
            
            $foutstr
=iconv("GB2312","utf-8",$fstr); //如果碰到 此文件中不存在双字节字符,则这个函数并不起作用

            
//$foutstr.="\n <!--脚注-->";  //这一句确保不存在双字节字符的文件也能得到转换,js文件经测试也可以。
                                        //<!-- -->形式的注释可以使用于php,css,html,css等文件中
                                        //本脚本(fileconv.php已经是utf-8编码保存的,所以加到转换后的字串里)
                                        //然而,这样不行,搞的配套的代码都不能运行了header语句大多就失效了。
            //echo $filePath."<BR>";
            fwrite($fp,$foutstr);
            
            $convertedFile
++;

            
//echo $filePath . "<<<<<<<<< $matches[0]<BR>";

            fclose($fp);

            }
     
    }
//是文本文件处理结束
    else{
        $notTextFile
++;
        
//echo $file."=========<BR>";
    }
}
function showdir($dir){
while ($file_name = readdir($dir)) {

if (($file_name != "."&& ($file_name != "..")) {
$file_list .
= "<li>$file_name";
if(is_dir($file_name))
showdir($file_name);
}

}













function utf8_probability(&$rawtextstr) {
$score 
= 0;
$i 
= 0;
$rawtextlen 
= 0;
$goodbytes 
= 0;
$asciibytes 
= 0;
$rawtextarray 
= preg_split("//",$rawtextstr,-1, PREG_SPLIT_NO_EMPTY); //转换成char数组,如果是php5,则可使用str_split
$rawtext = array();

//var_dump($rawtextarray);die;

for($i=0;$i<count($rawtextarray);$i++)
$rawtext[] 
= ord($rawtextarray[$i]); //ord(char)
//
 Maybe also use UTF8 Byte Order Mark(BOM): EF BB BF
//
BOM,某些utf8文件流的首3个字节,可以表示这个文件的编码方式

// Check to see if characters fit into acceptable ranges
//
print_r($rawtext);
$rawtextlen = strlen($rawtextstr);
for ($i = 0; $i < $rawtextlen; $i++) {
if ($rawtext[$i] < 0x80) { // One byte
    $asciibytes++// Ignore ASCII, can throw off count
else if (0xC0 <= $rawtext[$i] && $rawtext[$i] <= 0xDF && // Two bytes
$i+1 < $rawtextlen && 0x80 <= $rawtext[$i+1&& $rawtext[$i+1<= 0xBF) {
$goodbytes 
+= 2; $i++;
else if (0xE0 <= $rawtext[$i] && $rawtext[$i] <= 0xEF && // Three bytes
$i+2 < $rawtextlen && 0x80 <= $rawtext[$i+1&& $rawtext[$i+1<= 0xBF &&
0x80 <= $rawtext[$i+2&& $rawtext[$i+2<= 0xBF) {
$goodbytes 
+= 3; $i+=2;
}
}
//ascii is sub of utf8
if ($asciibytes == $rawtextlen) { return 0; }
$score 
= (int)(100 * ($goodbytes/($rawtextlen-$asciibytes)));
// If not above 98, reduce to zero to prevent coincidental matches
if ($score > 98) {
return $score;
else if ($score > 95 && $goodbytes > 30) {
// Allows for some (few) bad formed sequences
return $score;
else {
return 0;
}
}
?>

 

 

 

 

Posted on 2005-06-07 17:45  古代  阅读(3942)  评论(0编辑  收藏  举报