首页 > 开发 > PHP > 正文

PHP操作文件类实例代码

2024-05-04 21:52:52
字体:大 中 小
来源:转载
供稿:网友
发一个刚写完的文件操作类

第一次写类,写的不好,大家多提意见,不过不要骂我
刚才又加了两个功能,又加了注释,高手一定帮我看看哪有问题啊,谢谢
file.class.php
  1. <?php 
  2. /** 
  3. *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 
  4. * 列出目录里的文件等功能,路径后面别忘了加"/" 
  5. *  
  6. * @author 路人郝 
  7. * @copyright myself 
  8. * @link [url=http://www.phpr.cn]www.phpr.cn[/url] 
  9. *  
  10. */ 
  11. class fileoperate 
  12. { 
  13. var path;// 文件路径 
  14. var name;//文件名 
  15. var result;//对文件操作后的结果 
  16.  
  17. /** 
  18. * 本方法用来在path目录下创建name文件 
  19. * 
  20. * @param string path 
  21. * @param string name 
  22. */  
  23. function creat_file(path,name)//建立文件 
  24. {  
  25. filename=path.name; 
  26. if (file_exists(filename)) 
  27. { 
  28. echo "文件已经存在,请换个文件名"; 
  29. } 
  30. else  
  31. { 
  32. if (file_exists(path)) 
  33. { 
  34. touch(name); 
  35. rename(name,filename); 
  36. echo "文件建立成功 </br>"; 
  37. } 
  38. else{ 
  39. echo "目录不存在,请检查"; 
  40. } 
  41. } 
  42. } 
  43.  
  44. /** 
  45. * 本方法用来写文件,向path路径下name文件写入content内容,bool为写入选项,值为1时 
  46. * 接着文件原内容下继续写入,值为2时写入后的文件只有本次content内容 
  47. * 
  48. * @param string_type path 
  49. * @param string_type name 
  50. * @param string_type content 
  51. * @param bool_type bool 
  52. */ 
  53. function write_file(path,name,content,bool) //写文件 
  54. { 
  55. filename=path.name; 
  56. if (bool==1) { 
  57. if (is_writable(filename)) { 
  58. handle=fopen(filename,'a'); 
  59. if (!handle) { 
  60. echo "文件不能打开或文件不存在"; 
  61. exit; 
  62. } 
  63. result=fwrite(handle,content); 
  64. if (!result) { 
  65. echo "文件写入失败"; 
  66. } 
  67. echo "文件写入成功"; 
  68. fclose(handle); 
  69. } 
  70. else echo "文件不存在"; 
  71. } 
  72. if (bool==2) { 
  73. if (!file_exists(filename)) { 
  74. this->creat_file(path,name); 
  75. handle=fopen(filename,'a'); 
  76. if (fwrite(handle,content)); 
  77. echo "文件写入成功"; 
  78.  
  79. } 
  80. else { 
  81. unlink(filename); 
  82. this->creat_file(path,name); 
  83. this->write_file(path,name,content,1); 
  84. echo "文件修改成功"; 
  85. } 
  86. } 
  87.  
  88. } 
  89.  
  90. /** 
  91. * 本方法删除path路径下name文件 
  92. * 
  93. * @param string_type path 
  94. * @param string_type name 
  95. */ 
  96. function del_file(path,name){ //删除文件 
  97. filename=path.name; 
  98.  
  99. if (!file_exists(filename)) { 
  100. echo "文件不存在,请确认路径是否正确"; 
  101. } 
  102. else { 
  103. if (unlink(filename)){ 
  104. echo "文件删除成功"; 
  105. } 
  106. else echo "文件删除失败"; 
  107. } 
  108.  
  109. } 
  110.  
  111. /** 
  112. * 本方法用来修改path目录里name文件中的内容(可视) 
  113. * 
  114. * @param string_type path 
  115. * @param string_type name 
  116. */ 
  117. function modi_file(path,name){ //文件修改 
  118. filename=path.name; 
  119. handle=fopen(filename,'r+'); 
  120. content=file_get_contents(filename); 
  121. echo "<form id='form1' name='form1' action='modi_file.php' method='post'>"; 
  122. echo "<textarea name=content rows='10'>content</textarea>文件内容"; 
  123. echo "<p>"; 
  124. echo "<input type='text' name='filename' value=filename />文件路径<p>"; 
  125. echo "<input name=ss type=submit value=修改文件内容 />"; 
  126. echo "</form>"; 
  127. } 
  128.  
  129. /** 
  130. * 本方法用来复制name文件从spath到dpath 
  131. * 
  132. * @param string name 
  133. * @param string spath 
  134. * @param string dpath 
  135. */ 
  136. function copy_file(name,spath,dpath) //文件复制 
  137. { 
  138. filename=spath.name; 
  139. if (file_exists(filename)) { 
  140. handle=fopen(filename,'a'); 
  141. copy(filename,dpath.name); 
  142. if (file_exists(dpath.name)) 
  143. echo "文件复制成功"; 
  144. else echo "文件复制失败"; 
  145. } 
  146. else echo "文件不存在"; 
  147. } 
  148.  
  149. /** 
  150. * 本方法把name文件从spath移动到path路径 
  151. * 
  152. * @param string_type path 
  153. * @param string_type dirname 
  154. * @param string_type dpath 
  155. */ 
  156. function move_file(name,spath,dpath) //移动文件 
  157. { 
  158. filename=spath.name; 
  159. if (file_exists(filename)) { 
  160. result=rename(filename,dpath.name); 
  161. if (result==false or !file_exists(dpath)) 
  162. echo "文件移动失败或目的目录不存在"; 
  163. else  
  164. echo "文件移动成功"; 
  165. } 
  166. else { 
  167. echo "文件不存在,无法移动"; 
  168. } 
  169. } 
  170.  
  171. /** 
  172. * 本方法把filename文件重命名为newname文件 
  173. * 
  174. * @param string_type filename 
  175. * @param string_type newname 
  176. */ 
  177. function rename_file(filename,newname) { //文件或目录更名 
  178. path=pathinfo(filename); 
  179. dir=path['dirname']; //得到文件路径 
  180. newfilename=dir.newname; 
  181. if (file_exists(filename)) { //判断文件是否存在 
  182. result=rename(filename,newfilename); 
  183. if (result==true) 
  184. echo "文件更名成功"; 
  185. else  
  186. echo "文件更名失败"; 
  187. } 
  188. else  
  189. echo "文件不存在";  
  190. } 
  191.  
  192. /** 
  193. * 本方法用来列出目录里的文件或目录switch为1时按字母顺序列出所有目录和文件 
  194. * switch为2则只列出目录,switch为3时,只列出文件名 
  195. * 
  196. * @param string_type path 
  197. * @param int_type switch 
  198. */ 
  199. function list_filename(path,switch) //列出文件和目录名 
  200. { 
  201. if (file_exists(path)) { 
  202. dir=scandir(path); 
  203. if (switch==1){ //如果switch为1则按字母顺序列出所有目录和文件 
  204. for (i=0;i<=count(dir);i++) 
  205. { 
  206. if (dir[i]!="." and dir[i]!="..")  
  207. { 
  208. echo "dir[i]<br>"; 
  209. } 
  210. } 
  211. } 
  212. if (switch==2) //switch为2则只列出目录 
  213. { 
  214. for (i=0;i<=count(dir);i++) 
  215. { 
  216. x=is_dir(path.dir[i]); 
  217. if (dir[i]!="." and dir[i]!=".." and x==true) 
  218. { 
  219. echo "dir[i]<br>"; 
  220. } 
  221. } 
  222. } 
  223. if (switch==3) //switch为3时,只列出文件名 
  224. { 
  225. for (i=0;i<=count(dir);i++) 
  226. { 
  227. x=is_dir(path.dir[i]); 
  228. if (dir[i]!="." and dir[i]!=".." and x==false) 
  229. { 
  230. echo "dir[i]<br>"; 
  231. } 
  232. } 
  233. }  
  234. } 
  235. } 
  236.  
  237. /** 
  238. * 本方法在path目录下创建名为dirname的目录 
  239. * 
  240. * @param string_type path 
  241. * @param string_type dirname 
  242. */ 
  243. function creat_dir(path,dirname){ //创建目录 
  244. if (file_exists(path)) { 
  245. result=mkdir(path.dirname); 
  246. if (result) 
  247. echo "目录建立成功"; 
  248. else  
  249. echo "目录建立失败"; 
  250. } 
  251. else  
  252. echo "路径不存在,请重新输入"; 
  253. } 
  254.  
  255. /** 
  256. * 本方法删除pathname目录,包括该目录下所有的文件及子目录 
  257. * 
  258. * @param string_type pathname 
  259. */ 
  260. function del_dir(pathname){ //删除目录及目录里所有的文件夹和文件 
  261. if (!is_dir(pathname)) 
  262. {exit("你输入的不是一个目录,请检查") ;} 
  263. handle=opendir(pathname); 
  264. while ((fileordir=readdir(handle)) !== false) { 
  265. if (fileordir!="." && fileordir!="..") { 
  266. is_dir("pathname/fileordir")? 
  267. this->del_dir("pathname/fileordir"): 
  268. unlink("pathname/fileordir"); 
  269. } 
  270. } 
  271. if (readdir(handle) == false) 
  272. { 
  273. closedir(handle); 
  274. rmdir(pathname); 
  275. } 
  276. } 
  277. } 
  278. ?> 
modi_file.php
  1. <?php 
  2. include('file.class.php'); 
  3. content=_POST['content'];//得到文件内容 
  4. //echo content;// 
  5. filename=_POST['filename']; 
  6. path=pathinfo(filename); //得到文件路径 
  7. dir=path['dirname']."/"; //目录名  
  8. name=path['basename']; //文件名 
  9. op=new fileoperate(); 
  10. op->write_file("dir","name",content,2); 
  11. ?> 
test.php
 
  1. <?php 
  2. include('file.class.php'); 
  3. op=new fileoperate(); 
  4. //op->creat_file("./","aa3.txt"); 
  5. //op->del_file("./","aa3.txt"); 
  6. //b="fdfdsfdsfdsfdfdfdfdfdsfdsf1233456"; 
  7. //op->write_file("./","aa3.txt",b,1); 
  8. //op->modi_file("./","aa3.txt"); 
  9. //op->copy_file("aa3.txt","./","../a/"); 
  10. //op->move_file("aa3.txt","../","./"); 
  11. //op->rename_file("aa3.txt","aa2.txt"); 
  12. //op->list_filename("../",3); 
  13. //op->creat_dir("/wwwroot","wesley"); 
  14. //op->del_dir("/aaa/test");?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表