首頁  >  文章  >  後端開發  >  自己寫的一個php檔案及資料夾操作的類別(建立、刪除、移動、複製)

自己寫的一個php檔案及資料夾操作的類別(建立、刪除、移動、複製)

WBOY
WBOY原創
2016-07-25 09:07:38722瀏覽
  1. /**

  2. * 操縱檔案類別
  3. *
  4. * 範例:
  5. * FileUtil::createDir('a/1/2/3'); 測試建立資料夾建立一個a/1/2/3資料夾
  6. * FileUtil::createFile('b/1/2/3'); 測試建立檔案在b/1/2/資料夾下方建造一個3檔案
  7. * FileUtil::createFile('b /1/2/3.exe');測試建立檔案在b/1/2/資料夾下方建立一個3.exe檔
  8. * FileUtil::copyDir('b','d/e');測試複製資料夾建立d/e資料夾,把b資料夾下的內容複製進去
  9. * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe '); 測試複製檔案建立一個b/b資料夾,並把b/1/2資料夾中的3.exe檔複製進去
  10. * FileUtil::moveDir('a/','b/c' ); 測試移動資料夾建立一個b/c資料夾,並把a資料夾下的內容移動進去,並刪除a資料夾
  11. * FileUtil::moveFile('b/1/2/3.exe' ,'b/d/3.exe');測試移動檔案建立一個b/d資料夾,並把b/1/2中的3.exe移動進去
  12. * FileUtil::unlinkFile('b/d /3.exe'); 測試刪除檔案刪除b/d/3.exe檔案
  13. * FileUtil::unlinkDir('d');測試刪除資料夾刪除d資料夾
  14. */
  15. class FileUtil {
  16. /**
  17. * 建立資料夾
  18. *
  19. * @param string $aimUrl
  20. * @return viod
  21. */
  22. function createDir($aimUrl) {
  23. $aimUrl = str_replace('', '/', $aimUrl);
  24. $aimDir = '';
  25. $arr =explode('/', $aimUrl);
  26. $result = true;
  27. foreach ($arr as $str) {
  28. $aimDir .= $str . '/';
  29. if (!file_exists($aimDir)) {
  30. $result = mkdir($aimDir);
  31. }
  32. }
  33. return $result;
  34. }
  35. }
  36. return $result;
  37. }
  38. }
  39. return $result;
  40. }
  41. }
  42. return $result;
  43. } p>
  44. /**

  45. * 建立檔案
  46. *
  47. * @param string $aimUrl
  48. * @param boolean $overWrite 此參數控制是否覆寫原始檔案
  49. * @return boolean
  50. */
  51. function createFile($aimUrl, $overWrite = false) {
  52. if (file_exists($aimUrl) && $overWrite == false) {
  53. return false;
  54. } elseif (file_exists($aimUrl) && $overWrite == true) {
  55. FileUtil :: unlinkFile($aimUrl);
  56. }
  57. $aimDir = 目錄名($ targetUrl);
  58. FileUtil :: createDir($aimDir);
  59. touch($aimUrl);
  60. 回傳true;
  61. }
  62. /**

  63. * 行動資料夾
  64. *
  65. * @param string $oldDir
  66. * @param string $aimDir
  67. * @param boolean $overWrite 此參數控制是否覆寫原始檔案
  68. * @ return boolean
  69. */
  70. function moveDir($oldDir, $aimDir, $overWrite = false) {
  71. $aimDir = str_replace('', '/', $aimDir);
  72. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir 。 $oldDir : $oldDir 。 :: createDir($aimDir) );
  73. }
  74. @ $dirHandle = opendir($oldDir);
  75. if (!$dirHandle) {
  76. return false;
  77. }
  78. }
  79. while (false !== ($ file = readdir($dirHandle))) {
  80. if ($file == '.' || $file == '..') {
  81. continue;
  82. }
  83. if (!is_dir ($oldDir . $file)) {
  84. FileUtil :: moveFile($oldDir . $file, $aimDir . $file, $overWrite);
  85. } else {
  86. FileUtil> :: moveDir($oldDir . $file, $aimDir . $file, $overWrite);
  87. }
  88. }
  89. closedir($dirHandle);
  90. return rmdir($oldDir);
  91. }
  92. /**

  93. * 移動檔案
  94. *
  95. * @param string $fileUrl
  96. * @param string $aimUrl
  97. * @param boolean $overWrite 此參數控制是否覆寫原始檔案
  98. * @return boolean
  99. */
  100. function moveFile($fileUrl, $aimUrl, $overWrite = false) {
  101. if (!file_exists($fileUrl)) {
  102. return false;
  103. }
  104. if (file_exists($aimUrl) && $overWrite = false) {
  105. return false;
  106. } elseif (file_exists($aimUrl) &true $. {
  107. FileUtil :: unlinkFile($aimUrl );
  108. }
  109. $aimDir = dirname($aimUrl); FileUtil :: createDir($aimDir); FileUtil :: createDir($aimDir); $aimUrl); 回傳true; }
  110. /**

  111. * 刪除資料夾
  112. *
  113. * @param string $aimDir
  114. * @return boolean
  115. */
  116. function unlinkDir($aimDir) {
  117. $aimDir = str_replace('', '/', $aimDir);
  118. $aimDir = substr($aimDir, -1 ) == '/' ? $aimDir : $aimDir 。 false !== ($ file = readdir($dirHandle))) {
  119. if ($file == '.' || $file == '..') {
  120. continue;
  121. }
  122. if (!is_dir ($aimDir . $file)) {
  123. FileUtil :: unlinkFile($aimDir . $file);
  124. } else { FileUtil :: unlinkDir($aimDir . $. $ ;
  125. }
  126. }
  127. closedir($dirHandle);
  128. return rmdir($aimDir);
  129. }
  130. /**

  131. * 刪除檔案
  132. *
  133. * @param string $aimUrl
  134. * @return boolean
  135. */
  136. function unlinkFile($aimUrl) {
  137. if (file_exists($aimUrl)) {
  138. unlink($aimUrl);
  139. return true;
  140. } else {
  141. 返回; 🎜> }
  142. }
  143. /**

  144. * 複製資料夾
  145. *
  146. * @param string $oldDir
  147. * @param string $aimDir
  148. * @param boolean $overWrite 此參數控制是否覆蓋原檔案
  149. * @ return boolean
  150. */
  151. 函數copyDir($oldDir, $aimDir, $overWrite = false) {
  152. $aimDir = str_replace ('', '/', $aimDir);
  153. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir 。 $oldDir : $oldDir 。 :: createDir($aimDir) );
  154. }
  155. $dirHandle = opendir($oldDir);
  156. while (false !== ($file = readdir($dirHandle))) {
  157. if ($file == '.' || $file == '..') {
  158. 繼續;
  159. }
  160. if (!is_dir($oldDir . $file)) {
  161. FileUtil : : copyFile($oldDir . $file, $aimDir . $file, $overWrite);
  162. } else {
  163. FileUtil :: copyDir($oldDir . $file, $aimDir . $file, $overWrite); }
  164. }
  165. return closeir($dirHandle);
  166. }
  167. /**

  168. * 複製檔案
  169. *
  170. * @param string $fileUrl
  171. * @param string $aimUrl
  172. * @param boolean $overWrite 此參數控制是否覆寫原始檔案
  173. * @return boolean
  174. */
  175. function copyFile($fileUrl, $ , $overWrite = false) {
  176. if (!file_exists($fileUrl)) {
  177. return false;
  178. }
  179. if (file_exists($aimUrl) && $overWrite 🎜> if (file_exists($aimUrl) && $overWrite == false) { return false;
  180. } elseif (file_exists($aimUrl) && $overWrite == true) {
  181. FileUtil :: unlinkFile( $aimUrl);
  182. }
  183. $aimlinkFile( $aimUrl);
  184. }
  185. $aimDir =Uirname($aimDir =Uirname$aimD ;
  186. FileUtil :: createDir($aimDir);
  187. copy($fileUrl, $aimUrl);
  188. 回傳true;
  189. }
  190. }
  191. ?>
  192. 複製程式碼
  193. 另一種呼叫方式(非靜態調用):
  194. $fu = new FileUtil();
  195. $fu->copyFile('a/1/2/3', 'a/1 / 2/4');
複製程式碼

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