不完善,现在来说。但是改不多了。我以前用asp写过,但是没有写成类。这次从qs文学网下了很多。就写了这个类,from asia-storm。
<TEXTAREA NAME="info" ROWS="1" COLS="80"></TEXTAREA>
<FORM METHOD=POST ACTION="?">
文件名:
<input type='file' name='fname' size=90/><BR>
转换为目录:<INPUT TYPE="radio" NAME="changetype" value="dir">
转换为文件:<INPUT TYPE="radio" NAME="changetype" value="file" checked>
<INPUT TYPE="submit" value="转换">
</FORM>
<?php
$fname = str_replace("\\\\","/",$_POST['fname']);
$encode = $_POST['encode'];
$changeType = $_POST['changetype'];
//print_R($_POST);
if($fname <> ''){
$func = "cleanfile(\$filePath);";
if($changeType == 'file'){
cleanfile($fname);
}
else{
listDirTree(dirname($fname),$func);
}
}
class myfile{
var $file;
var $cleanfile;
var $dir;
var $extName;
function mymkdir($mainDir)
{if(strrpos($mainDir,"/")<> strlen($mainDir)) {$mainDir.="/";}
if(!is_dir($mainDir))
{
global $validFatherDir;
$this->getFatherDir($mainDir);
$fullLen=strlen($mainDir); //控制循环
//echo "需要创建的目录".$mainDir."长度为 $fullLen <BR>";die;
//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: $validFatherDir";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>";
$this->getFatherDir($fatherDir);
}
else
{
$validFatherDir= $fatherDir;
}
}
}
function changeExtname($file = '',$extName = '.htmlb'){
if($this->checkfile($file))
{
$foutdir=dirname($this->file);
$cleanfile=substr($this->file,strrpos($this->file,"/"));
$cleanfile2 =substr($cleanfile,0,strrpos($cleanfile,"."));
$cleanfile2 .= $extName;
//echo $foutdir.$cleanfile2;die;
$this->file = $foutdir.$cleanfile2;
return $this->file;
}
}
function getfile($file = ''){
if($this->checkfile($file))
{
$cleanfile=substr($this->file,strrpos($this->file,"/")+1);
$this->cleanfile = $cleanfile;
return $cleanfile;
}
}
function changeDir($file = '',$dir = ''){
if($this->checkfile($file))
{
$cleanfile=$this->getfile($file);
if(strrpos($dir,"/")<> strlen($dir)) {$dir.="/";}
if(!is_dir($dir)){$this->mymkdir($dir);}
return $dir.$cleanfile;
}
}
function checkfile($file){
if($file <> '')$this->file = $file;
if(is_file($this->file)){return true;}
else{
//echo $this->file;
return true;
}
}
}
function cleanfile($file){
$fstr=file_get_contents($file);
$myfile =new myfile;
$myfile->file=$file;
$foutfile = $myfile->changeExtname('',".html");
$outdir = dirname($myfile->file)."/out/";
if(!is_dir($outdir)){ $myfile->mymkdir($outdir); }
$foutfile = $outdir.$myfile->getfile();
//echo $foutfile;die;
$posf=strpos($fstr,"<span class='tpc_title'>");
$poset=strpos($fstr,"</span>",$posf);
$posec=strpos($fstr,"</span>",$poset+1);
//echo "$posf : $poset :$posec ";
$foutstr = substr($fstr,$posf,$posec+7-$posf);
if(strlen($foutstr)>100){
$fp = fopen ($foutfile,"wb");
fwrite($fp,$foutstr);
}
}
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;
}
/** 函数 listDirTree( $dirName = null )
* 功能 列出目录下所有文件及子目录
* 参数 $dirName 目录名称
* 返回 目录结构数组 false为失败
*/
function listDirTree( $dirName = null ,$func)
{//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>";
//echo $func;die;
eval($func);
//$tree[] = $file;
} //文件处理结束
}
}
closedir( $dh );
}
else
{
exit( "IBFileSystem: can not open directory $dirName.");
}
//返回当前的$tree
//return $tree;
}
else
{
exit( "IBFileSystem: $dirName is not a directory.");
}
}
function file2utf8($file = '',$encode="gb2312")
{
$file = str_replace("\\\\","/",$file);
$file = str_replace("\\","/",$file);
$fstr = file_get_contents($file);
//echo $file;die;
$cleanfile = substr($file,(strrpos($file,"/")+1));
$foutdir = dirname($file)."/out/";
$foutfile = $foutdir.$cleanfile;
if(!is_dir($foutdir))mkdir($foutdir);
//echo $foutfile;die;
$fp=fopen($foutfile,'wb');
$foutstr=iconv($encode,"utf-8",$fstr); //如果碰到 此文件中不存在双字节字符,则这个函数并不起作用
//fwrite($fp,$foutstr);
$javascript= "<script>info.value=".strlen($foutstr).";</script>";
echo $javascript;
fclose($fp);
}
?>
<SCRIPT LANGUAGE="JavaScript">
<!--
function sltall(slt)
{ objslt = document.getElementById(slt);
chlen = objslt.children.length;
for(x in objslt)
{
x.selected = true;
}
}
//-->
</SCRIPT>
<FORM METHOD=POST ACTION="?">
文件名:
<input type='file' name='fname' size=90/><BR>
转换为目录:<INPUT TYPE="radio" NAME="changetype" value="dir">
转换为文件:<INPUT TYPE="radio" NAME="changetype" value="file" checked>
<INPUT TYPE="submit" value="转换">
</FORM>
<?php
$fname = str_replace("\\\\","/",$_POST['fname']);
$encode = $_POST['encode'];
$changeType = $_POST['changetype'];
//print_R($_POST);
if($fname <> ''){
$func = "cleanfile(\$filePath);";
if($changeType == 'file'){
cleanfile($fname);
}
else{
listDirTree(dirname($fname),$func);
}
}
class myfile{
var $file;
var $cleanfile;
var $dir;
var $extName;
function mymkdir($mainDir)
{if(strrpos($mainDir,"/")<> strlen($mainDir)) {$mainDir.="/";}
if(!is_dir($mainDir))
{
global $validFatherDir;
$this->getFatherDir($mainDir);
$fullLen=strlen($mainDir); //控制循环
//echo "需要创建的目录".$mainDir."长度为 $fullLen <BR>";die;
//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: $validFatherDir";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>";
$this->getFatherDir($fatherDir);
}
else
{
$validFatherDir= $fatherDir;
}
}
}
function changeExtname($file = '',$extName = '.htmlb'){
if($this->checkfile($file))
{
$foutdir=dirname($this->file);
$cleanfile=substr($this->file,strrpos($this->file,"/"));
$cleanfile2 =substr($cleanfile,0,strrpos($cleanfile,"."));
$cleanfile2 .= $extName;
//echo $foutdir.$cleanfile2;die;
$this->file = $foutdir.$cleanfile2;
return $this->file;
}
}
function getfile($file = ''){
if($this->checkfile($file))
{
$cleanfile=substr($this->file,strrpos($this->file,"/")+1);
$this->cleanfile = $cleanfile;
return $cleanfile;
}
}
function changeDir($file = '',$dir = ''){
if($this->checkfile($file))
{
$cleanfile=$this->getfile($file);
if(strrpos($dir,"/")<> strlen($dir)) {$dir.="/";}
if(!is_dir($dir)){$this->mymkdir($dir);}
return $dir.$cleanfile;
}
}
function checkfile($file){
if($file <> '')$this->file = $file;
if(is_file($this->file)){return true;}
else{
//echo $this->file;
return true;
}
}
}
function cleanfile($file){
$fstr=file_get_contents($file);
$myfile =new myfile;
$myfile->file=$file;
$foutfile = $myfile->changeExtname('',".html");
$outdir = dirname($myfile->file)."/out/";
if(!is_dir($outdir)){ $myfile->mymkdir($outdir); }
$foutfile = $outdir.$myfile->getfile();
//echo $foutfile;die;
$posf=strpos($fstr,"<span class='tpc_title'>");
$poset=strpos($fstr,"</span>",$posf);
$posec=strpos($fstr,"</span>",$poset+1);
//echo "$posf : $poset :$posec ";
$foutstr = substr($fstr,$posf,$posec+7-$posf);
if(strlen($foutstr)>100){
$fp = fopen ($foutfile,"wb");
fwrite($fp,$foutstr);
}
}
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;
}
/** 函数 listDirTree( $dirName = null )
* 功能 列出目录下所有文件及子目录
* 参数 $dirName 目录名称
* 返回 目录结构数组 false为失败
*/
function listDirTree( $dirName = null ,$func)
{//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>";
//echo $func;die;
eval($func);
//$tree[] = $file;
} //文件处理结束
}
}
closedir( $dh );
}
else
{
exit( "IBFileSystem: can not open directory $dirName.");
}
//返回当前的$tree
//return $tree;
}
else
{
exit( "IBFileSystem: $dirName is not a directory.");
}
}
function file2utf8($file = '',$encode="gb2312")
{
$file = str_replace("\\\\","/",$file);
$file = str_replace("\\","/",$file);
$fstr = file_get_contents($file);
//echo $file;die;
$cleanfile = substr($file,(strrpos($file,"/")+1));
$foutdir = dirname($file)."/out/";
$foutfile = $foutdir.$cleanfile;
if(!is_dir($foutdir))mkdir($foutdir);
//echo $foutfile;die;
$fp=fopen($foutfile,'wb');
$foutstr=iconv($encode,"utf-8",$fstr); //如果碰到 此文件中不存在双字节字符,则这个函数并不起作用
//fwrite($fp,$foutstr);
$javascript= "<script>info.value=".strlen($foutstr).";</script>";
echo $javascript;
fclose($fp);
}
?>
<SCRIPT LANGUAGE="JavaScript">
<!--
function sltall(slt)
{ objslt = document.getElementById(slt);
chlen = objslt.children.length;
for(x in objslt)
{
x.selected = true;
}
}
//-->
</SCRIPT>