国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 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. var path;// 文件路徑 
  13. var name;//文件名 
  14. var result;//對文件操作后的結果 
  15.  
  16. /** 
  17. * 本方法用來在path目錄下創建name文件 
  18. * 
  19. * @param string path 
  20. * @param string name 
  21. */  
  22. function creat_file(path,name)//建立文件 
  23. {  
  24. filename=path.name; 
  25. if (file_exists(filename)) 
  26. echo "文件已經存在,請換個文件名"
  27. else  
  28. if (file_exists(path)) 
  29. touch(name); 
  30. rename(name,filename); 
  31. echo "文件建立成功 </br>"
  32. else
  33. echo "目錄不存在,請檢查"
  34.  
  35. /** 
  36. * 本方法用來寫文件,向path路徑下name文件寫入content內容,bool為寫入選項,值為1時 
  37. * 接著文件原內容下繼續寫入,值為2時寫入后的文件只有本次content內容 
  38. * 
  39. * @param string_type path 
  40. * @param string_type name 
  41. * @param string_type content 
  42. * @param bool_type bool 
  43. */ 
  44. function write_file(path,name,content,bool) //寫文件 
  45. filename=path.name; 
  46. if (bool==1) { 
  47. if (is_writable(filename)) { 
  48. handle=fopen(filename,'a'); 
  49. if (!handle) { 
  50. echo "文件不能打開或文件不存在"
  51. exit
  52. result=fwrite(handle,content); 
  53. if (!result) { 
  54. echo "文件寫入失敗"
  55. echo "文件寫入成功"
  56. fclose(handle); 
  57. else echo "文件不存在"
  58. if (bool==2) { 
  59. if (!file_exists(filename)) { 
  60. this->creat_file(path,name); 
  61. handle=fopen(filename,'a'); 
  62. if (fwrite(handle,content)); 
  63. echo "文件寫入成功"
  64.  
  65. else { 
  66. unlink(filename); 
  67. this->creat_file(path,name); 
  68. this->write_file(path,name,content,1); 
  69. echo "文件修改成功"
  70.  
  71.  
  72. /** 
  73. * 本方法刪除path路徑下name文件 
  74. * 
  75. * @param string_type path 
  76. * @param string_type name 
  77. */ 
  78. function del_file(path,name){ //刪除文件 
  79. filename=path.name; 
  80.  
  81. if (!file_exists(filename)) { 
  82. echo "文件不存在,請確認路徑是否正確"
  83. else { 
  84. if (unlink(filename)){ 
  85. echo "文件刪除成功"
  86. else echo "文件刪除失敗"
  87.  
  88.  
  89. /** 
  90. * 本方法用來修改path目錄里name文件中的內容(可視) 
  91. * 
  92. * @param string_type path 
  93. * @param string_type name 
  94. */ 
  95. function modi_file(path,name){ //文件修改 
  96. filename=path.name; 
  97. handle=fopen(filename,'r+'); 
  98. content=file_get_contents(filename); 
  99. echo "<form id='form1' name='form1' action='modi_file.php' method='post'>"
  100. echo "<textarea name=content rows='10'>content</textarea>文件內容"
  101. echo "<p>"
  102. echo "<input type='text' name='filename' value=filename />文件路徑<p>"
  103. echo "<input name=ss type=submit value=修改文件內容 />"
  104. echo "</form>"
  105.  
  106. /** 
  107. * 本方法用來復制name文件從spath到dpath 
  108. * 
  109. * @param string name 
  110. * @param string spath 
  111. * @param string dpath 
  112. */ 
  113. function copy_file(name,spath,dpath) //文件復制 
  114. filename=spath.name; 
  115. if (file_exists(filename)) { 
  116. handle=fopen(filename,'a'); 
  117. copy(filename,dpath.name); 
  118. if (file_exists(dpath.name)) 
  119. echo "文件復制成功"
  120. else echo "文件復制失敗"
  121. else echo "文件不存在"
  122.  
  123. /** 
  124. * 本方法把name文件從spath移動到path路徑 
  125. * 
  126. * @param string_type path 
  127. * @param string_type dirname 
  128. * @param string_type dpath 
  129. */ 
  130. function move_file(name,spath,dpath) //移動文件 
  131. filename=spath.name; 
  132. if (file_exists(filename)) { 
  133. result=rename(filename,dpath.name); 
  134. if (result==false or !file_exists(dpath)) 
  135. echo "文件移動失敗或目的目錄不存在"
  136. else  
  137. echo "文件移動成功"
  138. else { 
  139. echo "文件不存在,無法移動"
  140.  
  141. /** 
  142. * 本方法把filename文件重命名為newname文件 
  143. * 
  144. * @param string_type filename 
  145. * @param string_type newname 
  146. */ 
  147. function rename_file(filename,newname) { //文件或目錄更名 
  148. path=pathinfo(filename); 
  149. dir=path['dirname']; //得到文件路徑 
  150. newfilename=dir.newname; 
  151. if (file_exists(filename)) { //判斷文件是否存在 
  152. result=rename(filename,newfilename); 
  153. if (result==true) 
  154. echo "文件更名成功"
  155. else  
  156. echo "文件更名失敗"
  157. else  
  158. echo "文件不存在";  
  159.  
  160. /** 
  161. * 本方法用來列出目錄里的文件或目錄switch為1時按字母順序列出所有目錄和文件 
  162. * switch為2則只列出目錄,switch為3時,只列出文件名 
  163. * 
  164. * @param string_type path 
  165. * @param int_type switch 
  166. */ 
  167. function list_filename(path,switch//列出文件和目錄名 
  168. if (file_exists(path)) { 
  169. dir=scandir(path); 
  170. if (switch==1){ //如果switch為1則按字母順序列出所有目錄和文件 
  171. for (i=0;i<=count(dir);i++) 
  172. if (dir[i]!="." and dir[i]!="..")  
  173. echo "dir[i]<br>"
  174. if (switch==2) //switch為2則只列出目錄 
  175. for (i=0;i<=count(dir);i++) 
  176. x=is_dir(path.dir[i]); 
  177. if (dir[i]!="." and dir[i]!=".." and x==true) 
  178. echo "dir[i]<br>"
  179. if (switch==3) //switch為3時,只列出文件名 
  180. for (i=0;i<=count(dir);i++) 
  181. x=is_dir(path.dir[i]); 
  182. if (dir[i]!="." and dir[i]!=".." and x==false) 
  183. echo "dir[i]<br>"
  184. }  
  185.  
  186. /** 
  187. * 本方法在path目錄下創建名為dirname的目錄 
  188. * 
  189. * @param string_type path 
  190. * @param string_type dirname 
  191. */ 
  192. function creat_dir(path,dirname){ //創建目錄 
  193. if (file_exists(path)) { 
  194. result=mkdir(path.dirname); 
  195. if (result) 
  196. echo "目錄建立成功"
  197. else  
  198. echo "目錄建立失敗"
  199. else  
  200. echo "路徑不存在,請重新輸入"
  201.  
  202. /** 
  203. * 本方法刪除pathname目錄,包括該目錄下所有的文件及子目錄 
  204. * 
  205. * @param string_type pathname 
  206. */ 
  207. function del_dir(pathname){ //刪除目錄及目錄里所有的文件夾和文件 
  208. if (!is_dir(pathname)) 
  209. {exit("你輸入的不是一個目錄,請檢查") ;} 
  210. handle=opendir(pathname); 
  211. while ((fileordir=readdir(handle)) !== false) { 
  212. if (fileordir!="." && fileordir!="..") { 
  213. is_dir("pathname/fileordir")? 
  214. this->del_dir("pathname/fileordir"): 
  215. unlink("pathname/fileordir"); 
  216. if (readdir(handle) == false) 
  217. closedir(handle); 
  218. rmdir(pathname); 
  219. ?> 
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");?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇平县| 宜宾市| 淮南市| 江西省| 九江县| 泗水县| 灵宝市| 玉山县| 桂阳县| 宁城县| 彭泽县| 嘉善县| 延寿县| 肇州县| 绥化市| 西乌珠穆沁旗| 河津市| 若羌县| 腾冲县| 江山市| 鞍山市| 佛学| 常宁市| 淳安县| 纳雍县| 高碑店市| 宜春市| 乌鲁木齐县| 麟游县| 客服| 万源市| 防城港市| 湾仔区| 恩施市| 嘉鱼县| 东港市| 漳浦县| 沈丘县| 类乌齐县| 平泉县| 庄河市|