listfile.php
<?php
class listfile{var $callFunc; //操作函数,用于lisDirTree里的调用function listfile()
{
echo 'initialize filetree<BR>';
} // end func/** 函数 listDirTree( $dirName = null )
* 功能 对目录下所有文件及子目录下所有文件进行操作
* 参数 $dirName 目录名称
* 参数 $callFunc ,文件操作函数。
* 参数 $mainCallFunc ,默认操作函数。
*/function listDirTree( $dirName = null )
{//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] = $this->listDirTree( $filePath );
$this->listDirTree( $filePath );
}
else//为文件,进行处理
{ //echo "文件处理函数为 $this->callFunc <BR>";
eval($this->callFunc);//$tree[] = $file;
} //文件处理结束
}
}
closedir( $dh );}
else
{
exit( "IBFileSystem: can not open directory $dirName.");
}//返回当前的$tree
//return $tree;
}else
{
exit( "IBFileSystem: $dirName is not a directory.");
}
}//</listDirTree>/**
* mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
*/
function mkdirp($target) {
// If the path already exists && is a directory, all is well.
// If the path is not a directory, we've a problem.
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
} // Attempting to create the directory may clutter up our display.
if ( @mkdir($target) ) return true; // If the above failed, attempt to create the parent node, then try again.
if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target); return false;
}//</function mkdirp>}//</class listfile>
?>
<?php
class listfile{var $callFunc; //操作函数,用于lisDirTree里的调用function listfile()
{
echo 'initialize filetree<BR>';
} // end func/** 函数 listDirTree( $dirName = null )
* 功能 对目录下所有文件及子目录下所有文件进行操作
* 参数 $dirName 目录名称
* 参数 $callFunc ,文件操作函数。
* 参数 $mainCallFunc ,默认操作函数。
*/function listDirTree( $dirName = null )
{//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] = $this->listDirTree( $filePath );
$this->listDirTree( $filePath );
}
else//为文件,进行处理
{ //echo "文件处理函数为 $this->callFunc <BR>";
eval($this->callFunc);//$tree[] = $file;
} //文件处理结束
}
}
closedir( $dh );}
else
{
exit( "IBFileSystem: can not open directory $dirName.");
}//返回当前的$tree
//return $tree;
}else
{
exit( "IBFileSystem: $dirName is not a directory.");
}
}//</listDirTree>/**
* mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
*/
function mkdirp($target) {
// If the path already exists && is a directory, all is well.
// If the path is not a directory, we've a problem.
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
} // Attempting to create the directory may clutter up our display.
if ( @mkdir($target) ) return true; // If the above failed, attempt to create the parent node, then try again.
if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target); return false;
}//</function mkdirp>}//</class listfile>
?>
<?php
include_once("listfile.php");
class backupDir extends listfile {
var $ysDir; //需要操作的目录
var $backupPath;// 需要备份到的目录,如此目录不存在则自动创建
var $operateDirLen; //备份过程中用来截取子目录名的时候用function backup()
{ //echo $this->backupPath;//die;
if($this->readtag($this->backupPath)==0)//如果没有备份过,那么,执行备份
{
$this->callFunc="\$this->backupFile(\$file,\$filePath,\$dirName);";
$this->listDirTree($this->ysDir);
$this->setTag($this->backupPath); //设置备份标记
}
else
{//如果备份过了,那么,exit;
echo "backuped already";
}}//function backup endfunction backupFile($file,$filePath,$dirName)
{
$subdir = substr($dirName."/",$this->operateDirLen+1); echo "子目录:$subdir <BR>备份文件为: $bkfile <BR>原始文件为:$filePath<BR>"; $this->mkdirp($this->backupPath.$subdir); //创建子目录
$bkfile=$this->backupPath.$subdir.$file; $fstr=file_get_contents($filePath);
$fhandle=fopen($bkfile,"wb");
fwrite($fhandle,$fstr);
fclose($fhandle);
}function readtag( $backupDir )
{ //echo "$backupDir ";
if(!is_dir($backupDir)){
$this->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);
}
//$this->gb2utf8("tag.ini",$backupDir."tag.ini"); //将新建的tag.ini转为utf-8编码
return 0;
}
else //如果tag.ini已经存在,读取其中的配置
{
$backupTag=file_get_contents($backupDir."tag.ini");
$tagTree=$this->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;
}};//</class backup>
?>iconv.php<?php
include_once("listfile.php");
class fileconv extends listfile{ var $currentName;
var $changeType; //=dir 则操作目录,=file则操作文件。
var $operateFile; //如果this->changeType为file单个文件,那么这个值就是要操作的单个文件。
var $encode;
/**
* destEncode is the encode type you want to convert the text file to .
* @access public
*/
var $destEncode;
var $extname; //有效文本文件的扩展名。
var $operateDir; //需要操作的目录 function fileconv()
{ //初始化操作数据文件类型 $this->extname = ".php;.xml;html;.htm;.css;.txt;.js;.ini;.sql;.acp;.stx;.ctl;";
$this->encode = "gb2312";
$this->destEncode = "utf-8"; //初始化结果数据。 $this->utf8File=0;
$this->convertedFile=0;
$this->notTextFile=0;
}
function mainsub()
{ //echo $this->changeType;
//die;
if($this->changeType=="dir"){
$this->callFunc="\$this->gb2utf8(\$file,\$filePath);";
//echo 'ff'.$this->callFunc;
//die;
$this->listDirTree($this->operateDir);
}// />转换的为目录
else if($this->changeType=="file"){
$file=basename($this->operateFile);
$this->gb2utf8($file,$this->operateFile); }// />转换的为文件
$this->echoResult();
}//mainsub end
function echoResult()
{global $utf8File,$convertedFile,$notTextFile;
//echo (microtime()-$tm1)."秒<BR>";echo "<BR>utf8文件:".$this->utf8File;
echo "<BR>被转换的文件总数:".$this->convertedFile;
echo "<BR>非文本文件数目:".$this->notTextFile;}function gb2utf8($file,$filePath)
{
$extpos = strrpos($file,'.');
$fileext = substr($file,$extpos);
//echo $fileext.'<BR>';
preg_match("/".$fileext."/i",$this->extname, $matches); if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.......由数组$matches定义
{
$outfilename=$filePath; $fstr=file_get_contents($outfilename); $utf8score=$this->utf8_probability(&$fstr); if( 90<=$utf8score && $utf8score<=100)
{ $this->utf8File++;
//echo "<font color=red> $filePath is encoded with utf-8 already </font><BR>";//die;
}
else
{
$fp=fopen($outfilename,'wb');
$foutstr=iconv($this->encode,$this->destEncode,$fstr); //如果碰到 此文件中不存在双字节字符,则这个函数并不起作用
//echo "文件原来的编码格式:$this->encode";die;
echo $filePath."<BR>"; fwrite($fp,$foutstr);
$this->convertedFile++; //echo $filePath . "<<<<<<<<< $matches[0]<BR>"; fclose($fp); }
}//是文本文件处理结束
else{
$this->notTextFile++;
//echo $file."=========<BR>";
}
}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;
}
//if you want check just a few ,you may stop here with a score make.
//or you will be delayed when you meet lots of big files.
}
//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;
}
}}//class fileconv end?>
<?php
header('Content-Type: text/html; charset=utf-8');
echo "<PRE>";
include_once("listfile.php");
include_once("backup.php");
include_once("iconv.php");
$backupPath = "d:/usr/www/mcpu/";$fname=str_replace("\\\\","/",$_POST['fname']);
$encode=$_POST['encode'];
$changeType=$_POST['changetype'];if($changeType == "dir")$fname = dirname($fname);$time_start = getmicrotime();if(is_file($fname) || is_dir($fname))
{
echo "需要转换的文件目录:".dirname($fname) ."<BR>";
echo "已有的文件编码格式:$encode";$bk = new backupDir();
$fc = new fileconv();
$fc->changeType=$changeType; if(is_dir($fname))
{
$bk->ysDir = $fname;
$bk->backupPath=$backupPath;
$bk->operateDirLen = strlen($bk->ysDir);
$bk->backup(); $fc->operateDir=$fname;
}
else if(is_file($fname))
{
$file = basename($fname);
$filepath=$fname;
$bk->backupPath=dirname($fname)."/testbk/";
$bk->operateDirLen = strlen($bk->ysDir); echo "$file <BR> $filepath <BR> $bk->backupPath<BR>";
$bk->backupFile($file,$fname,$filepath);
$fc->operateFile=$fname; }
else{
echo "fatal error";die;
} $fc->callFunc="\$this->gb2utf8(\$file,\$filePath);";
if($encode){$fc->encode=$encode;}
$fc->mainsub();
}
else{
echo 'waiting for convert';
}
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
} $time_end = getmicrotime();
$time = $time_end - $time_start;echo "<BR><font color=red>It costs $time seconds </font><BR>";?><FORM METHOD=POST ACTION="fileconv.php">
文件名:如果你改此文件名为目录名,则转换整个目录
不支持中文目录名,不支持空格,目录名必须以字母,下划线,数字组成.
<input type='file' name='fname' size=90/>
原文件格式: <select name='encode'>
<option value='ISO-8859-1'> .iso8859-1 .latin1</option>
<option value='ISO-8859-2'> .iso8859-2 .latin2 .cen</option>
<option value='ISO-8859-3'> .iso8859-3 .latin3</option>
<option value='ISO-8859-4'> .iso8859-4 .latin4</option>
<option value='ISO-8859-5'> .iso8859-5 .latin5 .cyr .iso-ru</option>
<option value='ISO-8859-6'> .iso8859-6 .latin6 .arb</option>
<option value='ISO-8859-7'> .iso8859-7 .latin7 .grk</option>
<option value='ISO-8859-8'> .iso8859-8 .latin8 .heb</option>
<option value='ISO-8859-9'> .iso8859-9 .latin9 .trk</option>
<option value='ISO-2022-JP'> .iso2022-jp .jis</option>
<option value='ISO-2022-KR'> .iso2022-kr .kis</option>
<option value='ISO-2022-CN'> .iso2022-cn .cis</option>
<option value='Big5' > .Big5 .big5</option>
<option value='WINDOWS-1251'> .cp-1251 .win-1251</option>
<option value='CP866'> .cp866</option>
<option value='KOI8-r'> .koi8-r .koi8-ru</option>
<option value='KOI8-ru'> .koi8-uk .ua</option>
<option value='ISO-10646-UCS-2'> .ucs2</option>
<option value='ISO-10646-UCS-4'> .ucs4</option>
<option value='UTF-8'> .utf8</option>
<option value='GB2312' selected> .gb2312 .gb </option>
<option value='utf-7'> .utf7</option>
<option value='utf-8'> .utf8</option>
<option value='big5'> .big5 .b5</option>
<option value='EUC-TW'> .euc-tw</option>
<option value='EUC-JP'> .euc-jp</option>
<option value='EUC-KR'> .euc-kr</option>
<option value='shift_jis'> .sjis</option>
</select>转化后的格式:<select name='destencode'>
<option value='ISO-8859-1'> .iso8859-1 .latin1</option>
<option value='ISO-8859-2'> .iso8859-2 .latin2 .cen</option>
<option value='ISO-8859-3'> .iso8859-3 .latin3</option>
<option value='ISO-8859-4'> .iso8859-4 .latin4</option>
<option value='ISO-8859-5'> .iso8859-5 .latin5 .cyr .iso-ru</option>
<option value='ISO-8859-6'> .iso8859-6 .latin6 .arb</option>
<option value='ISO-8859-7'> .iso8859-7 .latin7 .grk</option>
<option value='ISO-8859-8'> .iso8859-8 .latin8 .heb</option>
<option value='ISO-8859-9'> .iso8859-9 .latin9 .trk</option>
<option value='ISO-2022-JP'> .iso2022-jp .jis</option>
<option value='ISO-2022-KR'> .iso2022-kr .kis</option>
<option value='ISO-2022-CN'> .iso2022-cn .cis</option>
<option value='Big5'> .Big5 .big5</option>
<option value='WINDOWS-1251'> .cp-1251 .win-1251</option>
<option value='CP866'> .cp866</option>
<option value='KOI8-r'> .koi8-r .koi8-ru</option>
<option value='KOI8-ru'> .koi8-uk .ua</option>
<option value='ISO-10646-UCS-2'> .ucs2</option>
<option value='ISO-10646-UCS-4'> .ucs4</option>
<option value='UTF-8' selected> .utf8</option>
<option value='GB2312'> .gb2312 .gb </option>
<option value='utf-7'> .utf7</option>
<option value='utf-8'> .utf8</option>
<option value='big5'> .big5 .b5</option>
<option value='EUC-TW'> .euc-tw</option>
<option value='EUC-JP'> .euc-jp</option>
<option value='EUC-KR'> .euc-kr</option>
<option value='shift_jis'> .sjis</option>
</select>转换为目录:<INPUT TYPE="radio" NAME="changetype" value="dir">
转换为文件:<INPUT TYPE="radio" NAME="changetype" value="file" checked>
<INPUT TYPE="submit" value="转换">
</FORM>
总算,可以在mozilla里回复了装了mozilla,opera
感觉firefox真的不错除 了浏览一些专为ie写的网页不行外,其他都很好