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

來源:互聯網
上載者:User
  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. * 建立檔案
  37. *
  38. * @param string $aimUrl
  39. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  40. * @return boolean
  41. */
  42. function createFile($aimUrl, $overWrite = false) {
  43. if (file_exists($aimUrl) && $overWrite == false) {
  44. return false;
  45. } elseif (file_exists($aimUrl) && $overWrite == true) {
  46. FileUtil :: unlinkFile($aimUrl);
  47. }
  48. $aimDir = dirname($aimUrl);
  49. FileUtil :: createDir($aimDir);
  50. touch($aimUrl);
  51. return true;
  52. }

  53. /**

  54. * 移動檔案夾
  55. *
  56. * @param string $oldDir
  57. * @param string $aimDir
  58. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  59. * @return boolean
  60. */
  61. function moveDir($oldDir, $aimDir, $overWrite = false) {
  62. $aimDir = str_replace('', '/', $aimDir);
  63. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
  64. $oldDir = str_replace('', '/', $oldDir);
  65. $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
  66. if (!is_dir($oldDir)) {
  67. return false;
  68. }
  69. if (!file_exists($aimDir)) {
  70. FileUtil :: createDir($aimDir);
  71. }
  72. @ $dirHandle = opendir($oldDir);
  73. if (!$dirHandle) {
  74. return false;
  75. }
  76. while (false !== ($file = readdir($dirHandle))) {
  77. if ($file == '.' || $file == '..') {
  78. continue;
  79. }
  80. if (!is_dir($oldDir . $file)) {
  81. FileUtil :: moveFile($oldDir . $file, $aimDir . $file, $overWrite);
  82. } else {
  83. FileUtil :: moveDir($oldDir . $file, $aimDir . $file, $overWrite);
  84. }
  85. }
  86. closedir($dirHandle);
  87. return rmdir($oldDir);
  88. }

  89. /**

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

  111. /**

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

  137. /**

  138. * 刪除檔案
  139. *
  140. * @param string $aimUrl
  141. * @return boolean
  142. */
  143. function unlinkFile($aimUrl) {
  144. if (file_exists($aimUrl)) {
  145. unlink($aimUrl);
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }

  151. /**

  152. * 複製檔案夾
  153. *
  154. * @param string $oldDir
  155. * @param string $aimDir
  156. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  157. * @return boolean
  158. */
  159. function copyDir($oldDir, $aimDir, $overWrite = false) {
  160. $aimDir = str_replace('', '/', $aimDir);
  161. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
  162. $oldDir = str_replace('', '/', $oldDir);
  163. $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
  164. if (!is_dir($oldDir)) {
  165. return false;
  166. }
  167. if (!file_exists($aimDir)) {
  168. FileUtil :: createDir($aimDir);
  169. }
  170. $dirHandle = opendir($oldDir);
  171. while (false !== ($file = readdir($dirHandle))) {
  172. if ($file == '.' || $file == '..') {
  173. continue;
  174. }
  175. if (!is_dir($oldDir . $file)) {
  176. FileUtil :: copyFile($oldDir . $file, $aimDir . $file, $overWrite);
  177. } else {
  178. FileUtil :: copyDir($oldDir . $file, $aimDir . $file, $overWrite);
  179. }
  180. }
  181. return closedir($dirHandle);
  182. }

  183. /**

  184. * 複製檔案
  185. *
  186. * @param string $fileUrl
  187. * @param string $aimUrl
  188. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  189. * @return boolean
  190. */
  191. function copyFile($fileUrl, $aimUrl, $overWrite = false) {
  192. if (!file_exists($fileUrl)) {
  193. return false;
  194. }
  195. if (file_exists($aimUrl) && $overWrite == false) {
  196. return false;
  197. } elseif (file_exists($aimUrl) && $overWrite == true) {
  198. FileUtil :: unlinkFile($aimUrl);
  199. }
  200. $aimDir = dirname($aimUrl);
  201. FileUtil :: createDir($aimDir);
  202. copy($fileUrl, $aimUrl);
  203. return true;
  204. }
  205. }
  206. ?>
複製代碼

另一種調用方式(非靜態調用):

  1. $fu = new FileUtil();
  2. $fu->copyFile('a/1/2/3', 'a/1/2/4');
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.