php专区

 首页 > php专区 > PHP应用 > php类库 > php文件操作类,建立,写入,删除,修改,复制,移动,创

php文件操作类,建立,写入,删除,修改,复制,移动,创

分享到:
【字体:大 中 小】
导读:
          php文件操作类,本文件操作类可实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 大家可参考参考 实例1,代码如下:?php *文件名:ClassFile php功能:面向对象,文件操作类(查...

php文件操作类,建立,写入,删除,修改,复制,移动,创建目录

php文件操作类,本文件操作类可实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录.大家可参考参考.

实例1,代码如下:

  1.  
  2.  /* 
  3.   文件名:ClassFile.php 
  4.   功能:面向对象,文件操作类(查看文件,删除文件,上传文件) 
  5.   作者:感染源 
  6.   时间:2007-1-11 
  7.  */ 
  8.  
  9.  class ClassFile 
  10.  { 
  11.   private $dir_name; 
  12.   private $file_name; 
  13.   private $dh; 
  14.   //private $file_path; 
  15.    
  16.    
  17.   //构造函数,打开工作文件目录 
  18.   function __construct($PDirName) 
  19.   { 
  20.    $this->dir_name = $PDirName; 
  21.    if(is_dir($this->dir_name)) 
  22.      
  23.     //打开目录 
  24.     if($this->dh = opendir($this->dir_name) or dir('路径错误,请重新检查路径!')); 
  25.   }//function __construct(); 
  26.    
  27.    
  28.   //关闭文件目录 
  29.   function __destruct() 
  30.   { 
  31.    //关闭目录 
  32.    closedir($this->dh); 
  33.   }//function __destruct(); 
  34.    
  35.    
  36.   //向当前文件夹添加文件,同时检查是否有重名的文件 
  37.   function AddFile($temp_file) 
  38.   { 
  39.    //读取目录 
  40.    while($this->file_name = readdir($this->dh)) 
  41.    { 
  42.     if($this->file_name==$temp_file)  //检查是否和已存在的文件重名 
  43.     { 
  44.      echo '文件重名,请更改文件名...'; 
  45.      exit; 
  46.     } 
  47.     else 
  48.     { 
  49.      //将临时文件复制到当前目录中 
  50.      $up_id = copy($temp_file,"$this->dir_name/$temp_file"); 
  51.      if(!$up_id) 
  52.      { 
  53.       echo '文件上传失败,请重新再传...'; 
  54.       exit; 
  55.      } 
  56.      else 
  57.      { 
  58.       unlink($temp_file);  //删除临时文件 
  59.        
  60.       echo ""; 
  61.       echo "alert("文件上传成功!");"; 
  62.       echo ""; 
  63.      }//if 
  64.     }//if 
  65.    }//while 
  66.   }//function AddFile(); 
  67.    
  68.    
  69.   //删除文件 
  70.   function DelFile($del_file) 
  71.   { 
  72.    if(is_file($del_file)) 
  73.    { 
  74.     //$path = "$this->dir_name/$del_file"; 
  75.     if(unlink($del_file)) 
  76.     { 
  77.      echo ""; 
  78.      echo "alert('成功删除文件!');"; 
  79.      echo ""; 
  80.     } 
  81.       
  82.    }//if 
  83.   }//function DelFile(); 
  84.    
  85.    
  86.   //读取文件 
  87.   function Read_File() 
  88.   { 
  89.     $result_file = array(); 
  90.     $i = 0; 
  91.    while($this->file_name = readdir($this->dh)) 
  92.    { 
  93.     if($this->file_name=='.'||$this->file_name=='..') 
  94.     {} 
  95.     else 
  96.     { 
  97.      $result_file[$i++] = "$this->dir_name/$this->file_name"; 
  98.     }//if 
  99.      
  100.    }//while 
  101.    return $result_file; //返回文件名数组 
  102.   }//function Read_File() 
  103.  }//Class ClassFile 
  104. ?> 

实例2,代码如下:

  1. /** 
  2. *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 
  3. * 列出目录里的文件等功能,路径后面别忘了加"/" 
  4. *  
  5. * @author 路人郝 
  6. * @copyright myself 
  7. *  
  8. */ 
  9. class fileoperate 
  10. { 
  11. var path;// 文件路径 
  12. var name;//文件名 
  13. var result;//对文件操作后的结果 
  14.  
  15. /** 
  16. * 本方法用来在path目录下创建name文件 
  17. * 
  18. * @param string path 
  19. * @param string name 
  20. */  
  21. function creat_file(path,name)//建立文件 
  22. {  
  23. filename=path.name; 
  24. if (file_exists(filename)) 
  25. { 
  26. echo "文件已经存在,请换个文件名"; 
  27. } 
  28. else  
  29. { 
  30. if (file_exists(path)) 
  31. { 
  32. touch(name); 
  33. rename(name,filename); 
  34. echo "文件建立成功 
    "
    ; 
  35. } 
  36. else{ 
  37. echo "目录不存在,请检查"; 
  38. } 
  39. } 
  40. } 
  41.  
  42. /** 
  43. * 本方法用来写文件,向path路径下name文件写入content内容,bool为写入选项,值为1时 
  44. * 接着文件原内容下继续写入,值为2时写入后的文件只有本次content内容 
  45. * 
  46. * @param string_type path 
  47. * @param string_type name 
  48. * @param string_type content 
  49. * @param bool_type bool 
  50. */ 
  51. function write_file(path,name,content,bool) //写文件 
  52. { 
  53. filename=path.name; 
  54. if (bool==1) { 
  55. if (is_writable(filename)) { 
  56. handle=fopen(filename,'a'); 
  57. if (!handle) { 
  58. echo "文件不能打开或文件不存在"; 
  59. exit; 
  60. } 
  61. result=fwrite(handle,content); 
  62. if (!result) { 
  63. echo "文件写入失败"; 
  64. } 
  65. echo "文件写入成功"; 
  66. fclose(handle); 
  67. } 
  68. else echo "文件不存在"; 
  69. } 
  70. if (bool==2) { 
  71. if (!file_exists(filename)) { 
  72. this->creat_file(path,name); 
  73. handle=fopen(filename,'a'); 
  74. if (fwrite(handle,content)); 
  75. echo "文件写入成功"; 
  76.  
  77. } 
  78. else { 
  79. unlink(filename); 
  80. this->creat_file(path,name); 
  81. this->write_file(path,name,content,1); 
  82. echo "文件修改成功"; 
  83. } 
  84. } 
  85.  
  86. } 
  87.  
  88. /** 
  89. * 本方法删除path路径下name文件 
  90. * 
  91. * @param string_type path 
  92. * @param string_type name 
  93. */ 
  94. function del_file(path,name){ //删除文件 
  95. filename=path.name; 
  96.  
  97. if (!file_exists(filename)) { 
  98. echo "文件不存在,请确认路径是否正确"; 
  99. } 
  100. else { 
  101. if (unlink(filename)){ 
  102. echo "文件删除成功"; 
  103. } 
  104. else echo "文件删除失败"; 
  105. } 
  106.  
  107. } 
  108.  
  109. /** 
  110. * 本方法用来修改path目录里name文件中的内容(可视) 
  111. * 
  112. * @param string_type path 
  113. * @param string_type name 
  114. */ 
  115. function modi_file(path,name){ //文件修改 
  116. filename=path.name; 
  117. handle=fopen(filename,'r+'); 
  118. content=file_get_contents(filename); 
  119. echo ""; 
  120. echo "content文件内容"; 
  121. echo "

    "; 

  122. echo "文件路径

    "; 

  123. echo ""; 
  124. echo ""; 
  125. } 
  126.  
  127. /** 
  128. * 本方法用来复制name文件从spath到dpath 
  129. * 
  130. * @param string name 
  131. * @param string spath 
  132. * @param string dpath 
  133. */ 
  134. function copy_file(name,spath,dpath) //文件复制 
  135. { 
  136. filename=spath.name; 
  137. if (file_exists(filename)) { 
  138. handle=fopen(filename,'a'); 
  139. copy(filename,dpath.name); 
  140. if (file_exists(dpath.name)) 
  141. echo "文件复制成功"; 
  142. else echo "文件复制失败"; 
  143. } 
  144. else echo "文件不存在"; 
  145. } 
  146.  
  147. /** 
  148. * 本方法把name文件从spath移动到path路径 
  149. * 
  150. * @param string_type path 
  151. * @param string_type dirname 
  152. * @param string_type dpath 
  153. */ 
  154. function move_file(name,spath,dpath) //移动文件 
  155. { 
  156. filename=spath.name; 
  157. if (file_exists(filename)) { 
  158. result=rename(filename,dpath.name); 
  159. if (result==false or !file_exists(dpath)) 
  160. echo "文件移动失败或目的目录不存在"; 
  161. else  
  162. echo "文件移动成功"; 
  163. } 
  164. else { 
  165. echo "文件不存在,无法移动"; 
  166. } 
  167. } 
  168. } 
  169. ?> 

实例3,代码如下:

这个比较高级了,不但可以实现文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录,还可以实现移动目录,copy,替换等操作,代码如下:

  1. class FSC{ 
  2. // 函数名: getfilesource 
  3. // 功能: 得到指定文件的内容 
  4. // 参数: $file 目标文件 
  5. // test passed 
  6. function getfilesource($file){ 
  7.     if($fp=fopen($file,'r')){ 
  8.         $filesource=fread($fp,filesize($file)); 
  9.         fclose($fp); 
  10.         return $filesource; 
  11.     } 
  12.     else 
  13.         return false; 
  14. } 
  15.  
  16. // 函数名: writefile 
  17. // 功能: 创建新文件,并写入内容,如果指定文件名已存在,那将直接覆盖 
  18. // 参数: $file -- 新文件名 
  19. // $source  文件内容 
  20. //test passed 
  21. function writefile($file,$source){ 
  22.     if($fp=fopen($file,'w')){ 
  23.         $filesource=fwrite($fp,$source); 
  24.         fclose($fp); 
  25.         return $filesource; 
  26.     } 
  27.     else 
  28.         return false; 
  29. } 
  30.  
  31. // 函数名: movefile 
  32. // 功能: 移动文件 
  33. // 参数: $file -- 待移动的文件名 
  34. // $destfile -- 目标文件名 
  35. // $overwrite 如果目标文件存在,是否覆盖.默认是覆盖. 
  36. // $bak 是否保留原文件 默认是不保留即删除原文件 
  37. // test passed 
  38. function movefile($file,$destfile,$overwrite=1,$bak=0){ 
  39.     if(file_exists($destfile)){ 
  40.         if($overwrite) 
  41.             unlink($destfile); 
  42.         else 
  43.             return false; 
  44.     } 
  45.     if($cf=copy($file,$destfile)){ 
  46.         if(!$bak) 
  47.             return(unlink($file)); 
  48.         } 
  49.     return($cf); 
  50. } 
  51.    
  52. // 函数名: movedir 
  53. // 功能: 这是下一涵数move的附助函数,功能就是移动目录 
  54.  
  55. function movedir($dir,$destdir,$overwrite=1,$bak=0){ 
  56.      @set_time_limit(600); 
  57.     if(!file_exists($destdir)) 
  58.         FSC::notfate_any_mkdir($destdir); 
  59.     if(file_exists($dir)&&(is_dir($dir))) 
  60.         { 
  61.         if(substr($dir,-1)!='/')$dir.='/'; 
  62.         if(file_exists($destdir)&&(is_dir($destdir))){ 
  63.         if(substr($destdir,-1)!='/')$destdir.='/'; 
  64.             $h=opendir($dir); 
  65.             while($file=readdir($h)){ 
  66.                 if($file=='.'||$file=='..') 
  67.                     { 
  68.                     continue; 
  69.                     $file=""; 
  70.                 } 
  71.                 if(is_dir($dir.$file)){ 
  72.                     if(!file_exists($destdir.$file)) 
  73.                         FSC::notfate_mkdir($destdir.$file); 
  74.                     else 
  75.                         chmod($destdir.$file,0777); 
  76.                     FSC::movedir($dir.$file,$destdir.$file,$overwrite,$bak); 
  77.                     FSC::delforder($dir.$file); 
  78.                     } 
  79.                 else 
  80.                 { 
  81.                     if(file_exists($destdir.$file)){ 
  82.                         if($overwrite)unlink($destdir.$file); 
  83.                         else{ 
  84.                             continue; 
  85.                             $file=""; 
  86.                             } 
  87.                     } 
  88.                     if(copy($dir.$file,$destdir.$file)) 
  89.                         if(!$bak) 
  90.                             if(file_exists($dir.$file)&&is_file($dir.$file)) 
  91.                                 @unlink($dir.$file); 
  92.                 } 
  93.             } 
  94.         } 
  95.         else 
  96.             return false; 
  97.     } 
  98.     else 
  99.         return false; 
  100. } 
  101.  
  102. // 函数名: move 
  103. // 功能: 移动文件或目录 
  104. // 参数: $file -- 源文件/目录 
  105. //       $path -- 目标路径 
  106. //       $overwrite -- 如是目标路径中已存在该文件时,是否覆盖移动 
  107. //                  --  默认值是1, 即覆盖 
  108. //       $bak  -- 是否保留备份(原文件/目录) 
  109.  
  110. function move($file,$path,$overwrite=1,$bak=0) 
  111.      { 
  112.     if(file_exists($file)){ 
  113.         if(is_dir($file)){ 
  114.             if(substr($file,-1)=='/')$dirname=basename(substr($file,0,strlen($file)-1)); 
  115.             else $dirname=basename($file); 
  116.             if(substr($path,-1)!='/')$path.='/'; 
  117.             if($file!='.'||$file!='..'||$file!='../'||$file!='./')$path.=$dirname; 
  118.             FSC::movedir($file,$path,$overwrite,$bak); 
  119.             if(!$bak)FSC::delforder($file); 
  120.             } 
  121.         else{ 
  122.             if(file_exists($path)){ 
  123.                 if(is_dir($path))chmod($path,0777); 
  124.                 else { 
  125.                     if($overwrite) 
  126.                         @unlink($path); 
  127.                     else 
  128.                         return false; 
  129.                 } 
  130.             } 
  131.             else 
  132.                 FSC::notfate_any_mkdir($path); 
  133.             if(substr($path,-1)!='/')$path.='/'; 
  134.             FSC::movefile($file,$path.basename($file),$overwrite,$bak); 
  135.         } 
  136.     } 
  137.     else 
  138.         return false; 
  139. } 
  140.  
  141. // 函数名: delforder 
  142. // 功能: 删除目录,不管该目录下是否有文件或子目录,全部删除哦,小心别删错了哦! 
  143. // 参数: $file -- 源文件/目录 
  144. //test passed 
  145.  
  146. function delforder($file) { 
  147.      chmod($file,0777); 
  148.      if (is_dir($file)) { 
  149.           $handle = opendir($file); 
  150.           while($filename = readdir($handle)) { 
  151.            if ($filename != "." && $filename != "..") 
  152.             { 
  153.                 FSC::delforder($file."/".$filename); 
  154.             } 
  155.           } 
  156.           closedir($handle); 
  157.           return(rmdir($file)); 
  158.      } 
  159.      else { 
  160.         unlink($file); 
  161.       } 
  162. } 
  163.  
  164. // 函数名: notfate_mkdir 
  165. // 功能: 创建新目录,这是来自php.net的一段代码.弥补mkdir的不足. 
  166. // 参数: $dir -- 目录名 
  167.  
  168.  
  169. function notfate_mkdir($dir,$mode=0777){ 
  170.     $u=umask(0); 
  171.     $r=mkdir($dir,$mode); 
  172.     umask($u); 
  173.     return $r; 
  174. } 
  175.  
  176. // 函数名: notfate_any_mkdir 
  177. // 功能: 创建新目录,与上面的notfate_mkdir有点不同,因为它多了一个any,即可以创建多级目录 
  178. //         如:notfate_any_mkdir("abc/abc/abc/abc/abc") 
  179. // 参数: $dirs -- 目录名 
  180.  
  181.  
  182. function notfate_any_mkdir($dirs,$mode=0777) 
  183. { 
  184.   if(!strrpos($dirs,'/')) 
  185.     { 
  186.       return(FSC::notfate_mkdir($dirs,$mode)); 
  187.   }else 
  188.       { 
  189.       $forder=explode('/',$dirs); 
  190.       $f=''; 
  191.       for($n=0;$n<count($forder);$n++) 
  192.           { 
  193.           if($forder[$n]=='') continue; 
  194.           $f.=((($n==0)&&($forder[$n]<>''))?(''):('/')).$forder[$n]; 
  195.           if(file_exists($f)){ 
  196.               chmod($f,0777); 
  197.               continue; 
  198.               } 
  199.           else 
  200.               { 
  201.               if(FSC::notfate_mkdir($f,$mode)) continue; 
  202.               else 
  203.                   return false; 
  204.           } 
  205.         } 
  206.         return true; 
  207.       } 
  208. } 
  209.  
  210. } 
  211. ?> 
分享到:
几个简单的php数据文件缓存类 - php类库
几个简单的php数据文件缓存类 数据文件缓存的做法我们常用的有php文件缓存与利用memcache来缓存数据,下面面我分别总结了memcache缓存数据与数据文件缓存有需要的朋友可参考参考. 1.对于一般的变量,把该变量变成php语言的格式,写到文件中,用时只要include这个文件就相当于加载了cache了. 2....
PHP中Memcache操作类使用方法 - php类库
PHP中Memcache操作类使用方法
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……