免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1154 | 回复: 0
打印 上一主题 下一主题

非常有用的文件操作类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-23 17:34 |只看该作者 |倒序浏览

    File 1.0 - A wrapper class to common PHP file operations
    Copyright ? 1999 CDI,
cdi@thewebmasters.net

    All Rights Reserved
*/
/******************************************************************
//以下为本文件类中用到的所有的方法和数据原形
METHODS
new File()
clear_cache( void )
is_sane( $fileName,$must_exist=0,$noSymLinks=0,$noDirs=0 )
read_file( $fileName )
strip_read( $fileName )
write_file( $fileName, $data )
copy_file( $oldFile, $newFile )
get_files( $path, $fileExt = 'ALL_FILES')
is_owner( $fileName, $uid = "" )
is_inGroup( $fileName, $gid = "" )
get_real_uid()
get_real_gid()
VARIABLES
ERROR (string)
BUFFER (integer, default -1 )
STATCACHE ( array, default NULL )
TEMPDIR ( string, default '/tmp' )
REALUID ( integer, default = -1 )
REALGID ( integer, default = -1 )
**********************************************************************/
Class File
{
    var $ERROR = "";
    var $BUFFER = -1;
    var $STATCACHE = array();
    var $TEMPDIR = '/tmp';
    var $REALUID = -1;
    var $REALGID = -1;
    function File ()
    {
global $php_errormsg;
return;
    }
    function clear_cache()
    {
    //清空 STATCACHE     
unset($this->STATCACHE);
$this->STATCACHE = array();
return true;
    }
    function is_sane($fileName = "", $must_exist = 0, $noSymLinks = 0, $noDirs = 0)
    {
$exists = false;
if(empty($fileName)) {    return false; }//如果文件名是空的,返回错误
if($must_exist != 0)
{
     if(!file_exists($fileName))//文件不存在
     {
   $this->ERROR = "is_sane: [$fileName] does not exist";
   return false;
     }
     $exists = true;
}
if($exists)
{
     if(!is_readable($fileName))//文件不可读
     {
   $this->ERROR = "is_sane: [$fileName] not readable";
   return false;
     }
     if($noDirs != 0)
     {
   if(is_dir($fileName))//给出的文件名实际是一个目录
   {
       $this->ERROR = "is_sane: [$fileName] is a directory";
       return false;
   }
     }
     if($noSymLinks != 0)
     {
   if(is_link($fileName))//如果文件是一个符号链接
   {
       $this->ERROR = "is_sane: [$fileName] is a symlink";
       return false;
   }
     }
} // end if exists
return true;   
    }
//    **************************************************************
    function read_file ($fileName = "" )
    {
$contents = "";
if(empty($fileName))//如果文件名为空
{
     $this->ERROR = "read_file: No file specified";
     return false;
}
if(!$this->is_sane($fileName,1,0,1))
{
     // Preserve the is_sane() error msg
     return false;
}
$fd = @fopen($fileName,"r");//打开文件
if( (!$fd) || (empty($fd)) )//打开文件出错
{
     $this->ERROR = "read_file: File error: [$php_errormsg]";
     return false;
}
$contents = fread($fd, filesize($fileName) );//读取文件内容到//$content中
fclose($fd);
       return $contents;
    }
//    **************************************************************
//    Read a file via fgetss(), which strips all php/html
//    from the file.
//  使用fgetss()来读取文件的内容,
//  它把所有的html和php标记都忽略了
    function strip_read ($fileName = "", $strip_cr = 0)
    {
if(empty($fileName))//如果文件为空
{
     $this->ERROR = "strip_read: No file specified";
     return false;
}
if(!$this->is_sane($fileName,1,0,1))
{
     // Preserve the error
     return false;
}
if($this->BUFFER > 0)
{
     $buffer = $this->BUFFER;
} else {
     $buffer = filesize($fileName);
}
$contents = "";
$fd = @fopen($fileName,"r");
if( (!$fd) || (empty($fd)) )
{
     $this->ERROR = "strip_read: File error: [$php_errormsg]";
     return false;
}
while(!feof($fd))
{
     $contents .= fgetss($fd,$buffer);
}
fclose($fd);
       return $contents;
    }
//    **************************************************************
    function write_file ($fileName,$Data)
    {
$tempDir = $this->TEMPDIR;//设定临时目录
$tempfile   = tempnam( $tempDir, "cdi" );//临时文件
if(!$this->is_sane($fileName,0,1,1))
{
     return false;
}
if (file_exists($fileName))
{
     if (!copy($fileName, $tempfile))
     {
   $this->ERROR = "write_file: cannot create backup file [$tempfile] :  [$php_errormsg]";
   return false;
     }
}
$fd = @fopen( $tempfile, "a" );
if( (!$fd) or (empty($fd)) )
{
     $myerror = $php_errormsg;
     unlink($tempfile);//删除文件
     $this->ERROR = "write_file: [$tempfile] access error [$myerror]";
     return false;
}
fwrite($fd, $Data);//向临时文件写入内容
fclose($fd);
if (!copy($tempfile, $fileName))//把临时文件复制到正式的文件中
{
     $myerror = $php_errormsg;   // Stash the error, see above
     unlink($tempfile);
     $this->ERROR = "write_file: Cannot copy file [$fileName] [$myerror]";
     return false;
}
unlink($tempfile);//删除临时文件
if(file_exists($tempfile))
{
     // Not fatal but it should be noted
     $this->ERROR = "write_file: Could not unlink [$tempfile] : [$php_errormsg]";
}
return true;
    }
//    **************************************************************
    function copy_file ($oldFile = "", $newFile = "")
    {
if(empty($oldFile))
{
     $this->ERROR = "copy_file: oldFile not specified";
     return false;
}
if(empty($newFile))
{
     $this->ERROR = "copy_file: newFile not specified";
     return false;
}
if(!$this->is_sane($oldFile,1,0,1))
{
     // preserve the error
     return false;
}
if(!$this->is_sane($newFile,0,1,1))
{
     // preserve it
     return false;
}
if (! (@copy($oldFile, $newFile)))//把$oldfile复制到$newfile
{
     $this->ERROR = "copy_file: cannot copy file [$oldFile] [$php_errormsg]";
     return false;
}
return true;
    }
//    **********************************************
    function get_files ($root_dir, $fileExt = 'ALL_FILES')
    {
$fileList = array();
if(!is_dir($root_dir))
{
     $this->ERROR = "get_files: Sorry, [$root_dir] is not a directory";
     return false;
}
if(empty($fileExt))
{
     $this->ERROR = "get_files: No file extensions specified";
     return false;
}
$open_dir = @opendir($root_dir);
if( (!$open_dir) or (empty($open_dir)) )
{
     $this->ERROR = "get_files: Failed to open dir [$root_dir] : $php_errormsg";
     return false;
}
$fileCount = 0;
while ( $file = readdir($open_dir))//读取目录的内容
{
     if( (!is_dir($file)) and (!empty($file)) )
     {
   if($fileExt == 'ALL_FILES')
   {
       $fileList[$fileCount] = $file;
       $fileCount++;
   }
   else
   {
       if(eregi(".\.($fileExt)$",$file))
       {
     $fileList[$fileCount] = $file;
     $fileCount++;
       }
   }
     }
}
closedir($open_dir);
return $fileList;
    }    // end get_files
    function is_owner($fileName, $uid = "")
    {
if(empty($uid))
{
     if($this->REALUID TEMPDIR;
   $tempFile = tempnam($tempDir,"cdi");
   if(!touch($tempFile))
   {
       $this->ERROR = "is_owner: Unable to create [$tempFile]";
       return false;
   }
   $stats = stat($tempFile);
   unlink($tempFile);
   $uid = $stats[4];
     }
     else
     {
   $uid = $this->REALUID;
     }
}
$fileStats = stat($fileName);
if( (empty($fileStats)) or (!$fileStats) )
{
     $this->ERROR = "is_owner: Unable to stat [$fileName]";
     return false;
}
$this->STATCACHE = $fileStats;
$owner = $fileStats[4];
if($owner == $uid)
{
     return true;
}
$this->ERROR = "is_owner: Owner [$owner] Uid [$uid] FAILED";
return false;
    }
    function is_inGroup($fileName, $gid = "")
    {
if(empty($gid))
{
     if($this->REALGID TEMPDIR;
   $tempFile = tempnam($tempDir,"cdi");
   if(!touch($tempFile))
   {
       $this->ERROR = "is_inGroup: Unable to create [$tempFile]";
       return false;
   }
   $stats = stat($tempFile);
   unlink($tempFile);
   $gid = $stats[5];
     }
     else
     {
   $gid = $this->REALGID;
     }
}
$fileStats = stat($fileName);//取得文件的状态
if( (empty($fileStats)) or (!$fileStats) )
{
     $this->ERROR = "is_inGroup: Unable to stat [$fileName]";
     return false;
}
$this->STATCACHE = $fileStats;
$group = $fileStats[5];
if($group == $gid)
{
     return true;
}
$this->ERROR = "is_inGroup: Group [$group] Gid [$gid] FAILED";
return false;
    }
    function get_real_uid()
    {
$tempDir = $this->TEMPDIR;
$tempFile = tempnam($tempDir,"cdi");
if(!touch($tempFile))
{
     $this->ERROR = "is_owner: Unable to create [$tempFile]";
     return false;
}
$stats = stat($tempFile);
unlink($tempFile);
$uid = $stats[4];
$gid = $stats[5];
$this->REALUID = $uid;
$this->REALGID = $gid;
return $uid;
    }
    function get_real_gid()
    {
$uid = $this->get_real_uid();
if( (!$uid) or (empty($uid)) )
{
     return false;
}
return $this->REALGID;
    }
}    // end class File
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13004/showart_70100.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP