Owy skrypt sluzy do kopiowania katalogow/plikow z jednego serwera ftp na drugi ... Potrzebuje aby ten skrypt podczas kopiowania nie nadpisywal plikow ... lecz aby je pomijal jesli istnieja...


  1. */
  2. //------------------------------------------------------------------------------
  3.  
  4. //------------------------------------------------------------------------------
  5. // START CONFIGURATION
  6. //------------------------------------------------------------------------------
  7.  // ftp address of destination server
  8.  // ex: ftp.mysite.com
  9. $ftp_server = "docelowyserwerftp.pl";
  10.  
  11.  // user name to access ftp destination server
  12.  // ex: administrator
  13. $ftp_user = "login.dla.serwera.docelowego";
  14.  
  15.  // password to access ftp destination server
  16.  // ex: secret
  17. $ftp_pass = "haslo.dla.ftp.docelowego";
  18.  
  19.  // *ABSOLUTE* (means "starting from the root") FTP path to the directory
  20.  // where local file system will be replicated.
  21.  // Path must begin with / and must *NOT* end with /
  22.  // ex: /html
  23. $ftp_initial_dir = "sciezka do jakiej ma kopiowac";
  24. //------------------------------------------------------------------------------
  25. // END Configuration
  26. //------------------------------------------------------------------------------
  27.  
  28. //uncomment this if you wish some debugging and comment subsequent line
  29. //error_reporting(E_ALL);
  30. error_reporting(0);
  31.  
  32. //******************************************************************************
  33. //  WARNING : You must not edit anything under this line
  34. // (of course, if you need, you can modify the code)
  35. //******************************************************************************
  36.  
  37.  
  38. //------------------------------------------------------------------------------
  39. // Utility functions
  40. //------------------------------------------------------------------------------
  41.  
  42. // function to forbid recursion through particular dirs
  43. function not_black_list($dir)
  44. {
  45.  $black_list = array();
  46.  if(in_array($dir, $black_list))
  47. return false;
  48.  else
  49. return true;
  50. }
  51.  
  52. // my ftp_mkdir ......
  53. function lello_mkdir($dir, $ftp_stream)
  54. {
  55.  global $ftp_server, $ftp_user, $ftp_pass, $ftp_initial_dir;
  56.  //removing trailing backslash
  57.  if(strrpos($dir, "/") == strlen($dir) && strlen($dir) > 0)
  58. $dir = substr($dir,0,strrpos($dir, "/")-1);
  59.  $tree = explode("/", $ftp_initial_dir.$dir);
  60.  
  61.  for($i=0; $i<count($tree); $i++)
  62. {
  63.  if($tree[$i] != "" && $tree[$i] != "/")
  64. {
  65.  if($i == 1)
  66. $base = "/";
  67.  else
  68. $base = "";
  69.  if(ftp_chdir($ftp_stream, $base.$tree[$i]) == false)
  70. {
  71.  ftp_mkdir($ftp_stream, $tree[$i]);
  72.  ftp_chdir($ftp_stream, $tree[$i]);
  73. }
  74. }
  75. }
  76. }
  77.  
  78. // ftp connection function
  79. function connetti()
  80. {
  81.  global $ftp_server, $ftp_user, $ftp_pass, $ftp_initial_dir;
  82.  $conn_id = ftp_connect("$ftp_server");
  83.  $login_result = ftp_login($conn_id, "$ftp_user", "$ftp_pass");
  84.  if ((!$conn_id) || (!$login_result))
  85. die("Ftp connection error!<br> server name: $ftp_server <br> user name: $ftp_user <br> password: $ftp_pass ");
  86.  else
  87. {
  88.  lello_mkdir("", $conn_id);
  89.  return $conn_id;
  90. }
  91. }
  92.  
  93. // my ftp_fput
  94. function lello_put($file, $conn_id, $fp, $dir)
  95. {
  96.  //as you can see i don't like using reg expr ;-) ... add estensions if needed
  97.  $img_type = array ("jpg","JPG","gif","GIF","png","PNG","jpeg","JPEG","swf","SWF","mov","MOV","mp3","MP3","mid","MID");
  98.  $ext = explode(".",$file);
  99.  if (in_array($ext[1],$img_type))
  100. $ftp_mode = FTP_BINARY;
  101.  else
  102. $ftp_mode = FTP_ASCII;
  103.  if(strpos($file, "%20") > 0)
  104. $file = str_replace("%20", " ", $file);
  105.  
  106.  if(ftp_fput($conn_id, $file, $fp, $ftp_mode) == false)
  107. echo("Error while transferring file: ".$dir.$file."<br>");
  108. }
  109.  
  110.  
  111. // file transferring function
  112. function carica($dir, $file, $conn_id)
  113. {
  114.  global $local_base, $ftp_server, $ftp_user, $ftp_pass, $ftp_initial_dir;
  115.  $fp = fopen($local_base.$dir."/".$file, "r");
  116.  if($fp == false)
  117. echo("cannot open file: ".$local_base.$dir."/".$file."<br>");
  118.  lello_mkdir($dir, $conn_id);
  119.  lello_put($file, $conn_id, $fp, $dir);
  120. }
  121. //------------------------------------------------------------------------------
  122. // END - Utility functions
  123. //------------------------------------------------------------------------------
  124.  
  125.  
  126. //getting local base dir
  127. $local_base = dirname($PATH_TRANSLATED);
  128. if(strrpos($local_base, "/") == strlen($local_base))
  129.  $local_base = substr($local_base,0,strrpos($local_base,"/"));
  130.  
  131. //ftp connection to destination server
  132. $ftp_stream = connetti();
  133.  
  134. //main recursion throught filesystem
  135. function RecurseDir($directory)
  136.  {
  137. global $ftp_stream, $local_base;
  138. if ($dir = opendir($local_base.$directory))
  139.  {
  140. $i = 0;
  141. while ($file = readdir($dir))
  142.  {
  143. if (($file != ".")&&($file != ".."))
  144.  {
  145. $tempDir = $directory."/".$file;
  146. if (is_dir($local_base."/".$tempDir))
  147.  {
  148. if(not_black_list($file))
  149.  RecurseDir($tempDir);
  150.  }
  151. else
  152.  {
  153. //file transfer
  154. carica($directory, $file, $ftp_stream);
  155.  }
  156.  $i++;
  157.  }
  158.  }//end while
  159. if ($i == 0)
  160.  {
  161. // empty directory
  162.  }
  163.  }
  164. else
  165.  {
  166. // directory could not be accessed
  167. echo "cannot open dir ".$local_base.$directory."<br>";
  168.  }
  169. }
  170. ?>
  171.  
  172. <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
  173. <html>
  174. <head>
  175.  <title>Hauling system</title>
  176. </head>
  177. <body>
  178. <b>Activity Report</b><br>
  179. <?php
  180. //main call
  181. RecurseDir();
  182.  
  183. //closing ftp connection
  184. ftp_quit($ftp_stream);
  185. ?>
  186. <br><br><b> ! DONE ! </b>
  187. </body>
  188. </html>
  189.  
  190. </body>
  191. </html>