Maison  >  Article  >  développement back-end  >  PHP操作文件类

PHP操作文件类

WBOY
WBOYoriginal
2016-07-25 09:11:40981parcourir
本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录
  1. /**
  2. *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录
  3. * 列出目录里的文件等功能,路径后面别忘了加"/"
  4. *
  5. * @author 路人郝
  6. * @copyright myself
  7. * @link www.phpr.cn
  8. *
  9. */
  10. class fileoperate
  11. {
  12. var path;// 文件路径
  13. var name;//文件名
  14. var result;//对文件操作后的结果
  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. * 本方法用来写文件,向path路径下name文件写入content内容,bool为写入选项,值为1时
  43. * 接着文件原内容下继续写入,值为2时写入后的文件只有本次content内容
  44. *
  45. * @param string_type path
  46. * @param string_type name
  47. * @param string_type content
  48. * @param bool_type bool
  49. */
  50. function write_file(path,name,content,bool) //写文件
  51. {
  52. filename=path.name;
  53. if (bool==1) {
  54. if (is_writable(filename)) {
  55. handle=fopen(filename,'a');
  56. if (!handle) {
  57. echo "文件不能打开或文件不存在";
  58. exit;
  59. }
  60. result=fwrite(handle,content);
  61. if (!result) {
  62. echo "文件写入失败";
  63. }
  64. echo "文件写入成功";
  65. fclose(handle);
  66. }
  67. else echo "文件不存在";
  68. }
  69. if (bool==2) {
  70. if (!file_exists(filename)) {
  71. this->creat_file(path,name);
  72. handle=fopen(filename,'a');
  73. if (fwrite(handle,content));
  74. echo "文件写入成功";
  75. }
  76. else {
  77. unlink(filename);
  78. this->creat_file(path,name);
  79. this->write_file(path,name,content,1);
  80. echo "文件修改成功";
  81. }
  82. }
  83. }
  84. /**
  85. * 本方法删除path路径下name文件
  86. *
  87. * @param string_type path
  88. * @param string_type name
  89. */
  90. function del_file(path,name){ //删除文件
  91. filename=path.name;
  92. if (!file_exists(filename)) {
  93. echo "文件不存在,请确认路径是否正确";
  94. }
  95. else {
  96. if (unlink(filename)){
  97. echo "文件删除成功";
  98. }
  99. else echo "文件删除失败";
  100. }
  101. }
  102. /**
  103. * 本方法用来修改path目录里name文件中的内容(可视)
  104. *
  105. * @param string_type path
  106. * @param string_type name
  107. */
  108. function modi_file(path,name){ //文件修改
  109. filename=path.name;
  110. handle=fopen(filename,'r+');
  111. content=file_get_contents(filename);
  112. echo "
    ";
  113. echo "文件内容";
  114. echo "

    ";

  115. echo "文件路径

    ";

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


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn