PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ festos/install/dircopy.php

http://festos.googlecode.com/
PHP | 66 lines | 47 code | 1 blank | 18 comment | 17 complexity | 9d173fd563162e6a3e0858d26ca9efb3 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. function dir_copy($srcdir, $dstdir, $offset = '', $verbose = false)
  3. {
  4. // A function to copy files from one directory to another one, including subdirectories and
  5. // nonexisting or newer files. Function returns number of files copied.
  6. // This function is PHP implementation of Windows xcopy A:\dir1\* B:\dir2 /D /E /F /H /R /Y
  7. // Syntaxis: [$returnstring =] dircopy($sourcedirectory, $destinationdirectory [, $offset] [, $verbose]);
  8. // Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);
  9. // Original by SkyEye. Remake by AngelKiha.
  10. // Linux compatibility by marajax.
  11. // ([danbrown AT php DOT net): *NIX-compatibility noted by Belandi.]
  12. // Offset count added for the possibilty that it somehow miscounts your files. This is NOT required.
  13. // Remake returns an explodable string with comma differentiables, in the order of:
  14. // Number copied files, Number of files which failed to copy, Total size (in bytes) of the copied files,
  15. // and the files which fail to copy. Example: 5,2,150000,\SOMEPATH\SOMEFILE.EXT|\SOMEPATH\SOMEOTHERFILE.EXT
  16. // If you feel adventurous, or have an error reporting system that can log the failed copy files, they can be
  17. // exploded using the | differentiable, after exploding the result string.
  18. //
  19. if(!isset($offset)) $offset=0;
  20. $num = 0;
  21. $fail = 0;
  22. $sizetotal = 0;
  23. $fifail = '';
  24. if(!is_dir($dstdir)) mkdir($dstdir);
  25. if($curdir = opendir($srcdir)) {
  26. while($file = readdir($curdir)) {
  27. if($file != '.' && $file != '..') {
  28. // $srcfile = $srcdir . '\\' . $file; # deleted by marajax
  29. // $dstfile = $dstdir . '\\' . $file; # deleted by marajax
  30. $srcfile = $srcdir . '/' . $file; # added by marajax
  31. $dstfile = $dstdir . '/' . $file; # added by marajax
  32. if(is_file($srcfile)) {
  33. if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
  34. if($ow > 0) {
  35. if($verbose) echo "Copying '$srcfile' to '$dstfile'...<br />";
  36. if(copy($srcfile, $dstfile)) {
  37. touch($dstfile, filemtime($srcfile)); $num++;
  38. chmod($dstfile, 0777); # added by marajax
  39. $sizetotal = ($sizetotal + filesize($dstfile));
  40. if($verbose) echo "OK\n";
  41. }
  42. else {
  43. echo "Error: File '$srcfile' could not be copied!<br />\n";
  44. $fail++;
  45. $fifail = $fifail.$srcfile.'|';
  46. }
  47. }
  48. }
  49. else if(is_dir($srcfile)) {
  50. $res = explode(',',$ret);
  51. // $ret = dircopy($srcfile, $dstfile, $verbose); # deleted by patrick
  52. $ret = dir_copy($srcfile, $dstfile, $verbose); # added by patrick
  53. $mod = explode(',',$ret);
  54. $imp = array($res[0] + $mod[0],$mod[1] + $res[1],$mod[2] + $res[2],$mod[3].$res[3]);
  55. $ret = implode(',',$imp);
  56. }
  57. }
  58. }
  59. closedir($curdir);
  60. }
  61. $red = explode(',',$ret);
  62. $ret = ($num + $red[0]).','.(($fail-$offset) + $red[1]).','.($sizetotal + $red[2]).','.$fifail.$red[3];
  63. return $ret;
  64. }
  65. ?>