php简单实用好用的文件及文件夹复制函数和工具类(创建、移动、复制、删除)
  1. function recurse_copy($src,$dst) {  // 原目录,复制到的目录
  2. $dir = opendir($src);
  3. @mkdir($dst);
  4. while(false !== ( $file = readdir($dir)) ) {
  5. if (( $file != '.' ) && ( $file != '..' )) {
  6. if ( is_dir($src . '/' . $file) ) {
  7. recurse_copy($src . '/' . $file,$dst . '/' . $file);
  8. }
  9. else {
  10. copy($src . '/' . $file,$dst . '/' . $file);
  11. }
  12. }
  13. }
  14. closedir($dir);
  15. }
  16. echo recurse_copy("原文件夹","目录文件夹");
复制

还有更流弊的工具类:

  1. <?php
  2. /**
  3. * 操纵文件类
  4. *
  5. * 例子:
  6. * FileUtil::createDir('a/1/2/3');                    测试建立文件夹 建一个a/1/2/3文件夹
  7. * FileUtil::createFile('b/1/2/3');                    测试建立文件        在b/1/2/文件夹下面建一个3文件
  8. * FileUtil::createFile('b/1/2/3.exe');             测试建立文件        在b/1/2/文件夹下面建一个3.exe文件
  9. * FileUtil::copyDir('b','d/e');                    测试复制文件夹 建立一个d/e文件夹,把b文件夹下的内容复制进去
  10. * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 测试复制文件        建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去
  11. * FileUtil::moveDir('a/','b/c');                    测试移动文件夹 建立一个b/c文件夹,并把a文件夹下的内容移动进去,并删除a文件夹
  12. * FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 测试移动文件        建立一个b/d文件夹,并把b/1/2中的3.exe移动进去
  13. * FileUtil::unlinkFile('b/d/3.exe');             测试删除文件        删除b/d/3.exe文件
  14. * FileUtil::unlinkDir('d');                      测试删除文件夹 删除d文件夹
  15. */
  16. class FileUtil {
  17. /**
  18. * 建立文件夹
  19. *
  20. * @param string $aimUrl
  21. * @return viod
  22. */
  23. function createDir($aimUrl) {
  24. $aimUrl = str_replace('', '/', $aimUrl);
  25. $aimDir = '';
  26. $arr = explode('/', $aimUrl);
  27. $result = true;
  28. foreach ($arr as $str) {
  29. $aimDir .= $str . '/';
  30. if (!file_exists($aimDir)) {
  31. $result = mkdir($aimDir);
  32. }
  33. }
  34. return $result;
  35. }
  36. /**
  37. * 建立文件
  38. *
  39. * @param string $aimUrl
  40. * @param boolean $overWrite 该参数控制是否覆盖原文件
  41. * @return boolean
  42. */
  43. function createFile($aimUrl, $overWrite = false) {
  44. if (file_exists($aimUrl) && $overWrite == false) {
  45. return false;
  46. } elseif (file_exists($aimUrl) && $overWrite == true) {
  47. FileUtil :: unlinkFile($aimUrl);
  48. }
  49. $aimDir = dirname($aimUrl);
  50. FileUtil :: createDir($aimDir);
  51. touch($aimUrl);
  52. return true;
  53. }
  54. /**
  55. * 移动文件夹
  56. *
  57. * @param string $oldDir
  58. * @param string $aimDir
  59. * @param boolean $overWrite 该参数控制是否覆盖原文件
  60. * @return boolean
  61. */
  62. function moveDir($oldDir, $aimDir, $overWrite = false) {
  63. $aimDir = str_replace('', '/', $aimDir);
  64. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
  65. $oldDir = str_replace('', '/', $oldDir);
  66. $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
  67. if (!is_dir($oldDir)) {
  68. return false;
  69. }
  70. if (!file_exists($aimDir)) {
  71. FileUtil :: createDir($aimDir);
  72. }
  73. @ $dirHandle = opendir($oldDir);
  74. if (!$dirHandle) {
  75. return false;
  76. }
  77. while (false !== ($file = readdir($dirHandle))) {
  78. if ($file == '.' || $file == '..') {
  79. continue;
  80. }
  81. if (!is_dir($oldDir . $file)) {
  82. FileUtil :: moveFile($oldDir . $file, $aimDir . $file, $overWrite);
  83. } else {
  84. FileUtil :: moveDir($oldDir . $file, $aimDir . $file, $overWrite);
  85. }
  86. }
  87. closedir($dirHandle);
  88. return rmdir($oldDir);
  89. }
  90. /**
  91. * 移动文件
  92. *
  93. * @param string $fileUrl
  94. * @param string $aimUrl
  95. * @param boolean $overWrite 该参数控制是否覆盖原文件
  96. * @return boolean
  97. */
  98. function moveFile($fileUrl, $aimUrl, $overWrite = false) {
  99. if (!file_exists($fileUrl)) {
  100. return false;
  101. }
  102. if (file_exists($aimUrl) && $overWrite = false) {
  103. return false;
  104. } elseif (file_exists($aimUrl) && $overWrite = true) {
  105. FileUtil :: unlinkFile($aimUrl);
  106. }
  107. $aimDir = dirname($aimUrl);
  108. FileUtil :: createDir($aimDir);
  109. rename($fileUrl, $aimUrl);
  110. return true;
  111. }
  112. /**
  113. * 删除文件夹
  114. *
  115. * @param string $aimDir
  116. * @return boolean
  117. */
  118. function unlinkDir($aimDir) {
  119. $aimDir = str_replace('', '/', $aimDir);
  120. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
  121. if (!is_dir($aimDir)) {
  122. return false;
  123. }
  124. $dirHandle = opendir($aimDir);
  125. while (false !== ($file = readdir($dirHandle))) {
  126. if ($file == '.' || $file == '..') {
  127. continue;
  128. }
  129. if (!is_dir($aimDir . $file)) {
  130. FileUtil :: unlinkFile($aimDir . $file);
  131. } else {
  132. FileUtil :: unlinkDir($aimDir . $file);
  133. }
  134. }
  135. closedir($dirHandle);
  136. return rmdir($aimDir);
  137. }
  138. /**
  139. * 删除文件
  140. *
  141. * @param string $aimUrl
  142. * @return boolean
  143. */
  144. function unlinkFile($aimUrl) {
  145. if (file_exists($aimUrl)) {
  146. unlink($aimUrl);
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. /**
  153. * 复制文件夹
  154. *
  155. * @param string $oldDir
  156. * @param string $aimDir
  157. * @param boolean $overWrite 该参数控制是否覆盖原文件
  158. * @return boolean
  159. */
  160. function copyDir($oldDir, $aimDir, $overWrite = false) {
  161. $aimDir = str_replace('', '/', $aimDir);
  162. $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
  163. $oldDir = str_replace('', '/', $oldDir);
  164. $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
  165. if (!is_dir($oldDir)) {
  166. return false;
  167. }
  168. if (!file_exists($aimDir)) {
  169. FileUtil :: createDir($aimDir);
  170. }
  171. $dirHandle = opendir($oldDir);
  172. while (false !== ($file = readdir($dirHandle))) {
  173. if ($file == '.' || $file == '..') {
  174. continue;
  175. }
  176. if (!is_dir($oldDir . $file)) {
  177. FileUtil :: copyFile($oldDir . $file, $aimDir . $file, $overWrite);
  178. } else {
  179. FileUtil :: copyDir($oldDir . $file, $aimDir . $file, $overWrite);
  180. }
  181. }
  182. return closedir($dirHandle);
  183. }
  184. /**
  185. * 复制文件
  186. *
  187. * @param string $fileUrl
  188. * @param string $aimUrl
  189. * @param boolean $overWrite 该参数控制是否覆盖原文件
  190. * @return boolean
  191. */
  192. function copyFile($fileUrl, $aimUrl, $overWrite = false) {
  193. if (!file_exists($fileUrl)) {
  194. return false;
  195. }
  196. if (file_exists($aimUrl) && $overWrite == false) {
  197. return false;
  198. } elseif (file_exists($aimUrl) && $overWrite == true) {
  199. FileUtil :: unlinkFile($aimUrl);
  200. }
  201. $aimDir = dirname($aimUrl);
  202. FileUtil :: createDir($aimDir);
  203. copy($fileUrl, $aimUrl);
  204. return true;
  205. }
  206. }
  207. ?>

php简单实用的操作文件工具类(创建、移动、复制、删除)的更多相关文章

  1. Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作

    有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn. ...

  2. java中文件操作的工具类

    代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...

  3. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  4. 简单了解Spring中常用工具类_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...

  5. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  6. android操作ini工具类

    package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...

  7. 简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下   本文实例讲述了简单实用的PHP防注 ...

  8. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  9. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

随机推荐

  1. Jenkins与Hudson的关系

    Jenkins的前身是Hudson(Sun开发),2010年从Hudson分支出来. 由于Sun被Oracle收购,Oracle拥有Hudson的商标所有权.分支出来的Jenkins将继续走open ...

  2. asp.net接收ajax请求参数时为空的现象

    如题,如果使用ajax请求asp.net后台时,如果使用jquery时,默认是添加了请求头,使后台能识别,并能通过Request对象进行获取. 但是如果你使用的是window.XMLHttpReque ...

  3. SPOJ GSS3 Can you answer these queries III

    Time Limit: 330MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  4. grid列的值格式化

    //列格式化 waf.defineCustomeClass("cellformatter.ratioFomatter", cellformatter.defaultFormatte ...

  5. 数据结构算法C语言实现(二十)--- 6.3.1遍历二叉树

    一.简述 二叉树的遍历主要是先序.中序.后序及对应的递归和非递归算法,共3x2=6种,其中后序非递归在实现上稍复杂一些.二叉树的遍历是理解和学习递归及体会栈的工作原理的绝佳工具! 此外,非递归所用的栈 ...

  6. shell处理mysql增、删、改、查

    引言     这几天做一个任务,比对两个数据表中的数据,昨天用PHP写了一个版本,但考虑到有的机器没有php或者php没有编译mysql扩展,就无法使用mysql系列的函数,脚本就无效了,今天写个sh ...

  7. Unity 单例写法

    借鉴自:http://www.cnblogs.com/CodeCabin/p/unity_global_manager.html 实现复杂一些的全局控制,如切换游戏关卡等操作,更常用的方式是使用单例类 ...

  8. 如何保持自己 fork 的项目和原始项目同步

    首先先通过 github 的 web 页面 fork 目标的项目 前提是自己已经设置好了git,并且配置了相应的权限 然后使用git clone命令在本地克隆自己 fork 的项目: git clon ...

  9. Groupby - collection processing

    Groupby - collection processing Iterator and Iterable have most of the most useful methods when deal ...

  10. Gated Recurrent Unit (GRU)公式简介

    update gate $z_t$: defines how much of the previous memory to keep around. \[z_t = \sigma ( W^z x_t+ ...