sspence65

PHP / FTP Sync (1 of 2)

Jun 21st, 2020
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //sync.php copies new files and folders from source to destination
  3. //created by Steve Spence
  4. // June 21, 2020
  5.  
  6.  
  7. echo 'Sync<br>';
  8.  
  9. ini_set('memory_limit', '-1');
  10. ini_set('max_execution_time', 300);
  11.    
  12. // set up basic connection
  13. $conn_id = ftp_connect($ipAddress); //ipaddress or FQDN
  14.  
  15. //check to see if server is responding
  16. if($conn_id){
  17.  
  18. // login with username and password
  19. $login_result = ftp_login($conn_id, $userName, $passWord);
  20.  
  21. $remote_dir=''; //ftp root on File Server (specified in ftp server config)
  22. $local_dir='c:/Apache24/htdocs/library/'; // local starting directory
  23.  
  24. // get the file list for /
  25. function getFiles($conn_id, $remote_dir, $local_dir) {
  26. $file_list = ftp_nlist($conn_id, $remote_dir);
  27. foreach ($file_list as $file)
  28. {
  29.   //echo "<br>$file";
  30.   $res = ftp_size($conn_id, $file);
  31.     $local_file = $local_dir.$file;
  32.     if ($res != -1) {
  33.         //echo "<br>$file";
  34.         if (file_exists($local_file)){
  35.             echo $local_file.'<br>';
  36.         } else {
  37.             ftp_get($conn_id, $local_file, $file, FTP_BINARY);
  38.         }
  39.     } else {
  40.         if (@!mkdir($local_file, 0777, true)) {
  41.            
  42.         }
  43.         getFiles($conn_id, $file, $local_dir);
  44.     }
  45. }
  46. }
  47.  
  48.  
  49.  
  50. getFiles($conn_id, $remote_dir, $local_dir);
  51.  
  52. //removeFiles($conn_id, $local_dir);
  53.  
  54. // close the connection
  55. ftp_close($conn_id);
  56.  
  57.  
  58.  
  59. } else {
  60.     echo 'FTP Server not responding';
  61. }
  62. ?>
RAW Paste Data Copied