递归文件/文件夹副本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归文件/文件夹副本相关的知识,希望对你有一定的参考价值。

  1. /**
  2. * Create a new directory, and the whole path.
  3. *
  4. * If the parent directory does not exists, we will create it,
  5. * etc.
  6. * @todo
  7. * - php5 mkdir functoin supports recursive, it should be used
  8. * @author baldurien at club-internet dot fr
  9. * @param string the directory to create
  10. * @param int the mode to apply on the directory
  11. * @return bool return true on success, false else
  12. * @previousNames mkdirs
  13. */
  14.  
  15. function makeAll($dir, $mode = 0777, $recursive = true) {
  16. if( is_null($dir) || $dir === "" ){
  17. return FALSE;
  18. }
  19.  
  20. if( is_dir($dir) || $dir === "/" ){
  21. return TRUE;
  22. }
  23. if( makeAll(dirname($dir), $mode, $recursive) ){
  24. return mkdir($dir, $mode);
  25. }
  26. return FALSE;
  27. }
  28.  
  29. /**
  30.  * Copies file or folder from source to destination, it can also do
  31.  * recursive copy by recursively creating the dest file or directory path if it wasn't exist
  32.  * Use cases:
  33.  * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination
  34.  * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it
  35.  * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest
  36.  * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest
  37.  * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name
  38.  * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name
  39.  * @todo
  40.  * - Should have rollback so it can undo the copy when it wasn't completely successful
  41.  * - It should be possible to turn off auto path creation feature f
  42.  * - Supporting callback function
  43.  * - May prevent some issues on shared enviroments : <a href="http://us3.php.net/umask" title="http://us3.php.net/umask">http://us3.php.net/umask</a>
  44.  * @param $source //file or folder
  45.  * @param $dest ///file or folder
  46.  * @param $options //folderPermission,filePermission
  47.  * @return boolean
  48.  */
  49. function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))
  50. {
  51. $result=false;
  52.  
  53. //For Cross Platform Compatibility
  54. if (!isset($options['noTheFirstRun'])) {
  55. $source=str_replace('\','/',$source);
  56. $dest=str_replace('\','/',$dest);
  57. $options['noTheFirstRun']=true;
  58. }
  59.  
  60. if (is_file($source)) {
  61. if ($dest[strlen($dest)-1]=='/') {
  62. if (!file_exists($dest)) {
  63. makeAll($dest,$options['folderPermission'],true);
  64. }
  65. $__dest=$dest."/".basename($source);
  66. } else {
  67. $__dest=$dest;
  68. }
  69. $result=copy($source, $__dest);
  70. chmod($__dest,$options['filePermission']);
  71.  
  72. } elseif(is_dir($source)) {
  73. if ($dest[strlen($dest)-1]=='/') {
  74. if ($source[strlen($source)-1]=='/') {
  75. //Copy only contents
  76. } else {
  77. //Change parent itself and its contents
  78. $dest=$dest.basename($source);
  79. @mkdir($dest);
  80. chmod($dest,$options['filePermission']);
  81. }
  82. } else {
  83. if ($source[strlen($source)-1]=='/') {
  84. //Copy parent directory with new name and all its content
  85. @mkdir($dest,$options['folderPermission']);
  86. chmod($dest,$options['filePermission']);
  87. } else {
  88. //Copy parent directory with new name and all its content
  89. @mkdir($dest,$options['folderPermission']);
  90. chmod($dest,$options['filePermission']);
  91. }
  92. }
  93.  
  94. $dirHandle=opendir($source);
  95. while($file=readdir($dirHandle))
  96. {
  97. if($file!="." && $file!="..")
  98. {
  99. $__dest=$dest."/".$file;
  100. $__source=$source."/".$file;
  101. //echo "$__source ||| $__dest<br />";
  102. if ($__source!=$dest) {
  103. $result=smartCopy($__source, $__dest, $options);
  104. }
  105. }
  106. }
  107. closedir($dirHandle);
  108.  
  109. } else {
  110. $result=false;
  111. }
  112. return $result;
  113. }

以上是关于递归文件/文件夹副本的主要内容,如果未能解决你的问题,请参考以下文章

目录的递归副本

如何递归地将目录复制到另一个目录并仅替换未更改的文件?

如何使用python执行非递归复制

Ubuntu SVN常用命令

HDFS——配额

打开文件时将文件的副本保存在特定文件夹中