+ -
当前位置:首页 → 问答吧 → 文件操作类及使用实列!

文件操作类及使用实列!

时间:2007-11-17

来源:互联网

为了支持原创版块,打击盗版的,所以我取消了原来那种上传附件的方式,直接采取贴出方式。以免有些人为了鱼目混珠。这是一个文件操作类,在下面还有一个怎样使用他的列子!
file.class.php页面:
复制内容到剪贴板
代码:
<?
class File
  {
          var $ERROR="";
        var $STATCACHE=array();
        var $TEMPDIR="/tmp";
        var $REALUID=-1;
        var        $REALGID=-1;
        

        /*****************************************************************
        function                          构造函数,不做任何事
        *****************************************************************/
        function File()
        {
            //在此可以初始化File类的变量
                return;
        }

        /*****************************************************************
        function                          删除类的STATCACHE数组变量的值并置为空数组
        *****************************************************************/

        function clear_cache()
        {
                unset($this->STATCACHE);//删除类的STATCACHE变量的值
                $this->STATCACHE=array();//设置类的STATCACHE变量为空数组
                return true;
        }
        
        /*****************************************************************
        function                          读取文件的内容,如果出错返回false,否则返回文件内容
        参数:$fileName                文件名称
        *****************************************************************/
        function read_file($fileName="")
        {
                $contents="";
                if(empty($fileName))//如果为空
                {
                        $this->ERROR="read_file : 文件名为空";
                        return false;
                }
                if(!file_exists($fileName))//如果文件不存在
                {
                        $this->ERROR="read_file : $fileName 不存在";
                        return false;
                }
                if(!is_readable($fileName))//如果文件不可读
                {
                        $this->ERROR="read_file : $fileName 不可读";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="read_file : $fileName 是文件夹";
                        return false;
                }
                $fd=@fopen($fileName,"r");
                if($fd)//如果指针有效
                {
                        $contents=fread($fd,filesize($fileName));
                }else{
                        $this->ERROR="read_file: File error: [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
                fclose($fd);
                return $contents;//返回文件内容
        }
        
        /*****************************************************************
        function                          读取文件的内容,如果出错返回false,否则返回去掉HTML标记的文件内容
        参数:$fileName                文件名称
        *****************************************************************/
        function strip_read($fileName="")
        {
                if(empty($fileName))//如果文件名为空,返回false
                {
                        $this->ERROR="strip_read:文件名为空";
                        return false;
                }
                if(!file_exists($fileName))//如果文件不存在
                {
                        $this->ERROR="read_file : $fileName 不存在";
                        return false;
                }
                if(!is_readable($fileName))//如果文件不可读
                {
                        $this->ERROR="read_file : $fileName 不可读";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="read_file : $fileName 是文件夹";
                        return false;
                }
                $contents="";
                $fd=@fopen($fileName,"r");
                if($fd)
                {
                        while(!feof($fd))
                        {
                                $contents .=fgetss($fd);//读取文件的所有内容并删除所有HTML标记
                        }
                }else{
                        $this->ERROR="strip_read: File error: [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
                fclose($fd);
                return $contents;//返回去掉所有HTML标记的文件内容
        }
        
        /*****************************************************************
        function                          以附加的方式写文件文件的内容
        参数:$fileName                文件名称
        参数:$Data                        要写的字符数据
        *****************************************************************/
        function write_file($fileName,$Data)
        {
                $tempDir=$this->TEMPDIR;//设置临时文件夹
                $tempfile=tempnam($tempDir,"cdi");//在临时文件夹下创建一个唯一的不重复的临时文件
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="write_file : $fileName 是文件夹";
                        return false;
                }
                if(is_link($fileName))//如果文件是链接
                {
                        $this->ERROR="write_file : $fileName 是链接";
                        return false;
                }
                if(file_exists($fileName))//如果文件存在
                {
                        if(!copy($fileName,$tempfile))//如果复制文件到临时文件,如果复制失败
                        {
                                $this->ERROR="write_file: 不能创建备份文件 [$tempfile]:[".$GLOBALS[php_errormsg]."]";
                                return false;
                        }
                }
                //复制成功,打开临时文件以追加
                $fd=@fopen($tempfile,"a");
                if($fd)//如果文件指针合法
                {
                        fwrite($fd,$Data);
                }else{//否则删除临时文件
                        $myerror=$GLOBALS[php_errormsg];
                        unlink($tempfile);
                        $this->ERROR="write_file:[$tempfile] access error [$myerror]";
                        return false;
                }
                fclose($fd);//关闭文件指针,临时文件中保存的将是最新内容
                if(!copy($tempfile,$fileName))
                {//复制临时文件覆盖原文件如果失败,删除临时文件,返回false
                        $myerror=$GLOBALS[php_errormsg];
                        unlink($tempfile);
                        $this->ERROR="write_file: Cannot copy file [$fileName] [$myerror]";
                        return false;
                }
                unlink($tempfile);//复制成功,删除临时文件
                if(file_exists($tempfile))//如果临时文件存在,表示删除失败,并返回true,指出错误
                {
                        $this->ERROR="write_file:Could not unlink [$tempfile] [".$GLOBALS[php_errormsg]."]";
                }
                return true;
        }
        
        /*****************************************************************
        function                          复制文件
        参数:$oldFile                原文件
        参数:$newFile                新文件
        *****************************************************************/
        function copy_file($oldFile="",$newFile="")
        {
                if(empty($oldFile))
                {//如果原文件为空
                        $this->ERROR="copy_file: oldFile 文件为空";
                        return false;
                }
                if(empty($newFile))
                {//如果新文件为空
                        $this->ERROR="copy_file: newFile 文件为空";
                        return false;
                }

                if(!file_exists($oldFile))//如果原文件不存在
                {
                        $this->ERROR="copy_file : $oldFile 文件不存在";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="copy_file : $oldFile 文件是文件夹";
                        return false;
                }

                if(is_link($newFile))//如果新文件是链接
                {
                        $this->ERROR="copy_file : $newFile 文件是链接";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="copy_file : $newFile 文件是文件夹";
                        return false;
                }

                if(!(@copy($oldFile,$newFile)))//复制文件,如果失败,返回false
                {
                        $this->ERROR="copy_file: 不能复制文件 [$oldFile] [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
                return true;//复制成功
        }
        
        /*****************************************************************
        function                          得到文件夹下所有文件列表并以数组返回,失败返回false
        参数:$root_dir                文件夹
        参数:$fileExt                扩展名限制(如果为ALL_FILES表示对所有文件进行操作)
        *****************************************************************/
        function get_files($root_dir,$fileExt='ALL_FILES')
        {
                $fileList=array();//定义文件列表空数组
                if(!is_dir($root_dir))//如果$root_dir不是文件夹,返回false
                {
                        $this->ERROR="get_files:对不起,[$root_dir] 不是文件夹";
                        return false;
                }
                $open_dir=@opendir($root_dir);//打开文件夹$root_dir
               
                if($open_dir)//如果文件夹操作句柄合法
                {
                        $fileCount=0;
                        while($file=readdir($open_dir))//遍历文件夹中的所有文档,并将文件名(不含路径)放于$file变量中
                        {
                                if((!is_dir($file)) and (!empty($file)))
                                {//如果$file不是文件夹且不为空
                                        if($fileExt=="ALL_FILES")
                                        {//如果$fileExt为ALL_FILES,则将所有文件加入数组fileList中
                                                $fileList[$fileCount]=$file;
                                                $fileCount++;
                                        }else{//否则
                                                if(eregi(".\.($fileExt)[code]<?
class File
  {
          var $ERROR="";
        var $STATCACHE=array();
        var $TEMPDIR="/tmp";
        var $REALUID=-1;
        var        $REALGID=-1;
        

        /*****************************************************************
        function                          构造函数,不做任何事
        *****************************************************************/
        function File()
        {
            //在此可以初始化File类的变量
                return;
        }

        /*****************************************************************
        function                          删除类的STATCACHE数组变量的值并置为空数组
        *****************************************************************/

        function clear_cache()
        {
                unset($this->STATCACHE);//删除类的STATCACHE变量的值
                $this->STATCACHE=array();//设置类的STATCACHE变量为空数组
                return true;
        }
        
        /*****************************************************************
        function                          读取文件的内容,如果出错返回false,否则返回文件内容
        参数:$fileName                文件名称
        *****************************************************************/
        function read_file($fileName="")
        {
                $contents="";
                if(empty($fileName))//如果为空
                {
                        $this->ERROR="read_file : 文件名为空";
                        return false;
                }
                if(!file_exists($fileName))//如果文件不存在
                {
                        $this->ERROR="read_file : $fileName 不存在";
                        return false;
                }
                if(!is_readable($fileName))//如果文件不可读
                {
                        $this->ERROR="read_file : $fileName 不可读";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="read_file : $fileName 是文件夹";
                        return false;
                }
                $fd=@fopen($fileName,"r");
                if($fd)//如果指针有效
                {
                        $contents=fread($fd,filesize($fileName));
                }else{
                        $this->ERROR="read_file: File error: [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
                fclose($fd);
                return $contents;//返回文件内容
        }
        
        /*****************************************************************
        function                          读取文件的内容,如果出错返回false,否则返回去掉HTML标记的文件内容
        参数:$fileName                文件名称
        *****************************************************************/
        function strip_read($fileName="")
        {
                if(empty($fileName))//如果文件名为空,返回false
                {
                        $this->ERROR="strip_read:文件名为空";
                        return false;
                }
                if(!file_exists($fileName))//如果文件不存在
                {
                        $this->ERROR="read_file : $fileName 不存在";
                        return false;
                }
                if(!is_readable($fileName))//如果文件不可读
                {
                        $this->ERROR="read_file : $fileName 不可读";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="read_file : $fileName 是文件夹";
                        return false;
                }
                $contents="";
                $fd=@fopen($fileName,"r");
                if($fd)
                {
                        while(!feof($fd))
                        {
                                $contents .=fgetss($fd);//读取文件的所有内容并删除所有HTML标记
                        }
                }else{
                        $this->ERROR="strip_read: File error: [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
                fclose($fd);
                return $contents;//返回去掉所有HTML标记的文件内容
        }
        
        /*****************************************************************
        function                          以附加的方式写文件文件的内容
        参数:$fileName                文件名称
        参数:$Data                        要写的字符数据
        *****************************************************************/
        function write_file($fileName,$Data)
        {
                $tempDir=$this->TEMPDIR;//设置临时文件夹
                $tempfile=tempnam($tempDir,"cdi");//在临时文件夹下创建一个唯一的不重复的临时文件
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="write_file : $fileName 是文件夹";
                        return false;
                }
                if(is_link($fileName))//如果文件是链接
                {
                        $this->ERROR="write_file : $fileName 是链接";
                        return false;
                }
                if(file_exists($fileName))//如果文件存在
                {
                        if(!copy($fileName,$tempfile))//如果复制文件到临时文件,如果复制失败
                        {
                                $this->ERROR="write_file: 不能创建备份文件 [$tempfile]:[".$GLOBALS[php_errormsg]."]";
                                return false;
                        }
                }
                //复制成功,打开临时文件以追加
                $fd=@fopen($tempfile,"a");
                if($fd)//如果文件指针合法
                {
                        fwrite($fd,$Data);
                }else{//否则删除临时文件
                        $myerror=$GLOBALS[php_errormsg];
                        unlink($tempfile);
                        $this->ERROR="write_file:[$tempfile] access error [$myerror]";
                        return false;
                }
                fclose($fd);//关闭文件指针,临时文件中保存的将是最新内容
                if(!copy($tempfile,$fileName))
                {//复制临时文件覆盖原文件如果失败,删除临时文件,返回false
                        $myerror=$GLOBALS[php_errormsg];
                        unlink($tempfile);
                        $this->ERROR="write_file: Cannot copy file [$fileName] [$myerror]";
                        return false;
                }
                unlink($tempfile);//复制成功,删除临时文件
                if(file_exists($tempfile))//如果临时文件存在,表示删除失败,并返回true,指出错误
                {
                        $this->ERROR="write_file:Could not unlink [$tempfile] [".$GLOBALS[php_errormsg]."]";
                }
                return true;
        }
        
        /*****************************************************************
        function                          复制文件
        参数:$oldFile                原文件
        参数:$newFile                新文件
        *****************************************************************/
        function copy_file($oldFile="",$newFile="")
        {
                if(empty($oldFile))
                {//如果原文件为空
                        $this->ERROR="copy_file: oldFile 文件为空";
                        return false;
                }
                if(empty($newFile))
                {//如果新文件为空
                        $this->ERROR="copy_file: newFile 文件为空";
                        return false;
                }

                if(!file_exists($oldFile))//如果原文件不存在
                {
                        $this->ERROR="copy_file : $oldFile 文件不存在";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="copy_file : $oldFile 文件是文件夹";
                        return false;
                }

                if(is_link($newFile))//如果新文件是链接
                {
                        $this->ERROR="copy_file : $newFile 文件是链接";
                        return false;
                }
                if(is_dir($fileName))//如果文件是文件夹
                {
                        $this->ERROR="copy_file : $newFile 文件是文件夹";
                        return false;
                }

                if(!(@copy($oldFile,$newFile)))//复制文件,如果失败,返回false
                {
                        $this->ERROR="copy_file: 不能复制文件 [$oldFile] [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
                return true;//复制成功
        }
        
        /*****************************************************************
        function                          得到文件夹下所有文件列表并以数组返回,失败返回false
        参数:$root_dir                文件夹
        参数:$fileExt                扩展名限制(如果为ALL_FILES表示对所有文件进行操作)
        *****************************************************************/
        function get_files($root_dir,$fileExt='ALL_FILES')
        {
                $fileList=array();//定义文件列表空数组
                if(!is_dir($root_dir))//如果$root_dir不是文件夹,返回false
                {
                        $this->ERROR="get_files:对不起,[$root_dir] 不是文件夹";
                        return false;
                }
                $open_dir=@opendir($root_dir);//打开文件夹$root_dir
               
                if($open_dir)//如果文件夹操作句柄合法
                {
                        $fileCount=0;
                        while($file=readdir($open_dir))//遍历文件夹中的所有文档,并将文件名(不含路径)放于$file变量中
                        {
                                if((!is_dir($file)) and (!empty($file)))
                                {//如果$file不是文件夹且不为空
                                        if($fileExt=="ALL_FILES")
                                        {//如果$fileExt为ALL_FILES,则将所有文件加入数组fileList中
                                                $fileList[$fileCount]=$file;
                                                $fileCount++;
                                        }else{//否则
                                                if(eregi(".\.($fileExt)[        DISCUZ_CODE_0        ]quot;,$file))//如果$file是以.$fileExt结尾
                                                {//则将这样的文件加入数组fileList中
                                                        $fileList[$fileCount]=$file;
                                                        $fileCount++;
                                                }
                                        }
                                }
                        }
                        closedir($open_dir);//关闭文件夹
                        return $fileList;//返回数组
                }else{//不合法,返回false
                        $this->ERROR="get_files: 打开文件夹失败 [$root_dir] [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
        }

  }        
?>
quot;,$file))//如果$file是以.$fileExt结尾
                                                {//则将这样的文件加入数组fileList中
                                                        $fileList[$fileCount]=$file;
                                                        $fileCount++;
                                                }
                                        }
                                }
                        }
                        closedir($open_dir);//关闭文件夹
                        return $fileList;//返回数组
                }else{//不合法,返回false
                        $this->ERROR="get_files: 打开文件夹失败 [$root_dir] [".$GLOBALS[php_errormsg]."]";
                        return false;
                }
        }

  }        
?>[/code]

使用类的列子(usefileclass.php):
复制内容到剪贴板
代码:
"http://www.w3.org/TR/html4/loose.dtd">
无标题文include("file.class.php");
$File=new File;
        $oldFile="old.html";
        $newFile="new.html";
        if(!$File->copy_file($oldFile,$newFile))
        {
                echo "$File->ERROR
\n";
                exit;
        }
        $contents=$File->read_file($newFile);
        echo "$contents\n";
        unlink($newFile);
        if(!$File->write_file($newFile,$contents))
        {
                echo "$File->ERROR
\n";
                exit;
        }
        $plainText=$File->strip_read($newFile);
        echo "$plainText\n";
?>
[ 本帖最后由 chaizhiyong 于 2007-11-16 16:45 编辑 ]

作者: chaizhiyong   发布时间: 2007-11-16

长的代码一般人都认为很难,可是细心读下来也不尽然,支持一下!

作者: dx_andy   发布时间: 2007-11-17

刚才忘了说了,写点建议!
为什么你检测$file是否正确时,不另写一个函数来操作,比如 check_file()
让所有的判断都在这里面,这样可以减少不少代码,也精简一下程序,可读性,维护性都提高不少!

作者: dx_andy   发布时间: 2007-11-17

呵呵,谢谢你的建议!

作者: chaizhiyong   发布时间: 2007-11-19

怎么不把源码公布啊!我现在真的要很想要个这样的东西!

作者: hejingke   发布时间: 2007-12-28


-_-||| 虽然做过-_-|||

但还是建议LZ 放包!论坛显示有问题-_-

作者: MoHock   发布时间: 2007-12-28

热门下载

更多