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

php 复制粘贴覆盖文件的更多相关文章

  1. windows doc命令复制粘贴文件

    # 删除前端文件 rmdir /s/q E:\yuanbo2019\public\static del E:\yuanbo2019\public\index.html @echo off ::被复制的 ...

  2. 个人永久性免费-Excel催化剂功能第44波-可见区域复制粘贴不覆盖隐藏内容

    Excel的复制粘贴操作,每天都在进行,若其中稍能提升一点效率,长久来说,实在是很可观的效率提升. Excel自带的复制粘贴功能,若复制的数据源或粘贴的目标位置中有隐藏的行列内容,简单一个复制粘贴充满 ...

  3. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  4. Android Studio 复制粘贴图片到drawable文件夹没有效果 - 解决方法

    我想放一些图片到drawable文件夹里面,但是简单的复制文件,粘贴文件,或者拖拽文件,都不起作用.不知道为什么,之前是可以的,突然就不行了. 解决方案 在drawable文件夹的目录上右键,选择Re ...

  5. 无法向Windows服务器复制粘贴文件

    在向服务器拷贝文件的时候卡死,直接任务管理器结束应用程序,但是随之引发一个问题,就是之后不能从本地向服务器拷贝文件了,只能服务器自己复制粘贴. 解决办法重启rdpclip.exe,先在任务管理器中结束 ...

  6. (已解决)在linux的虚拟机上安装vmware tools(实现windows与虚拟机之间的文件交互复制粘贴)

    Linux下需要安装VMware Tools工具来实现主机和虚拟机直接文件复制粘贴功能,安装方法如下: ①点击虚拟机VM菜单栏--虚拟机--安装VMware Tools. ②然后RedHat系统中弹出 ...

  7. VMWare12虚拟机实现主客机间的文件拖拽(复制粘贴)和文件夹共享

    版本: 主机:Windows 7 64位旗舰版 虚拟机: VMWare 12 + Windows 7 64位旗舰版 VMWare pro 12 + Ubuntu16.04LTS 64位 注:由于VMW ...

  8. Emacs复制粘贴乱码问题以及修改当前文件编码

    编码修改: 为了和Linux兼容,win环境下将emacs编码修改为utf-8,随意复制了其他关于emacs编码的配置,如下: (setq locale-coding-system 'utf-8) ( ...

  9. Linux 复制、移动覆盖文件不提示

    # vi ~/.bashrc   如果你看到如下内容,以下命令都会用别名执行了,就是说自动加了 -i 参数 alias rm='rm -i'alias cp='cp -i'alias mv='mv - ...

随机推荐

  1. 通过OpenSSL解析X509证书基本项

    在之前的文章"通过OpenSSL解码X509证书文件"里.讲述了怎样使用OpenSSL将证书文件解码,得到证书上下文结构体X509的方法. 以下我们接着讲述怎样通过证书上下文结构体 ...

  2. 多类别分类问题由 confusion matrix 到分类准确率(accuracy)的计算

    conf_mat = confusionmat(y_true, y_pred); % 首先根据数据集上的真实 label 值,和训练算法给出的预测 label 值, % 计算 confusion ma ...

  3. 企业部署Linux应用将拥有更低的TCO

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  4. Vue Invalid handler for event "": got undefined

    原因:绑定的方法不是放在methods:{}里.比如我把绑定的函数写在了computed:{}里就会报这个错.

  5. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~

  6. HDU——T 2824 The Euler function

    http://acm.hdu.edu.cn/showproblem.php?pid=2824 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  7. enq: TX - row lock contention故障处理一则

    一个非常easy的问题,之所以让我对这个问题进行总结.一是由于没我想象的简单,在处理的过程中遇到了一些磕磕碰碰,甚至绕了一些弯路.二是引发了我对故障处理时的一些思考. 6月19日,下午5点左右.数据库 ...

  8. android屏幕适配方案

    曾经看了android的屏幕适配方案,有非常多种.当中自己用到的一种是:先找一款主流的分辨率的android机,如:1080*1920的分辨率做基准,然后在这个基准上.调整好一切布局.图片.适配其它手 ...

  9. 21.Spring Boot 使用Java代码创建Bean并注册到Spring中

    转自:https://blog.csdn.net/catoop/article/details/50558333

  10. 18. springboot整合jsp

    转自:https://blog.csdn.net/u012562943/article/details/51836729