首頁  >  文章  >  後端開發  >  PHP操作檔案類

PHP操作檔案類

WBOY
WBOY原創
2016-07-25 09:11:40981瀏覽
本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录
  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. * 本方法用来复制name文件从spath到dpath
  121. *
  122. * @param string name
  123. * @param string spath
  124. * @param string dpath
  125. */
  126. function copy_file(name,spath,dpath) //文件复制
  127. {
  128. filename=spath.name;
  129. if (file_exists(filename)) {
  130. handle=fopen(filename,'a');
  131. copy(filename,dpath.name);
  132. if (file_exists(dpath.name))
  133. echo "文件复制成功";
  134. else echo "文件复制失败";
  135. }
  136. else echo "文件不存在";
  137. }
  138. /**
  139. * 本方法把name文件从spath移动到path路径
  140. *
  141. * @param string_type path
  142. * @param string_type dirname
  143. * @param string_type dpath
  144. */
  145. function move_file(name,spath,dpath) //移动文件
  146. {
  147. filename=spath.name;
  148. if (file_exists(filename)) {
  149. result=rename(filename,dpath.name);
  150. if (result==false or !file_exists(dpath))
  151. echo "文件移动失败或目的目录不存在";
  152. else
  153. echo "文件移动成功";
  154. }
  155. else {
  156. echo "文件不存在,无法移动";
  157. }
  158. }
  159. }
  160. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn