sspence65

PHP / FTP Sync (2 of 2)

Jun 21st, 2020
276
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None
  1. <?php
  2. //prune.php removes orphaned files from destination
  3. //created by Steve Spence
  4. //June 21, 2020
  5.  
  6.  
  7. echo 'Prune<br>';
  8.  
  9. // set up basic connection
  10. $conn_id = ftp_connect($ipAddress);
  11.  
  12.  
  13.  
  14.  
  15. // login with username and password
  16. $login_result = ftp_login($conn_id, $userName, $passWord);
  17.  
  18.  
  19. $path2 = ''; //local relative directory
  20. $path1 = ''; //remote relative directory
  21.  
  22. $objects_dest = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path2), RecursiveIteratorIterator::SELF_FIRST);
  23. foreach($objects_dest as $name_source => $object){
  24.    
  25.     $findme = '\.';
  26.     $pos = strpos($name_source, $findme);
  27.     if ($pos === false) {
  28.    
  29.         if (is_file($name_source)) {
  30.        
  31.         $name_dest = substr($name_source, 14);
  32.         echo $name_dest.'<br>';
  33.        
  34.             if (file_exists($name_source)){
  35.                 echo $name_source.'<br>';
  36.                 $res = ftp_size($conn_id, $name_dest);
  37.  
  38.                 if ($res != -1) {
  39.                     echo "size of $name_source is $res bytes<br>";
  40.                 } else {
  41.                     echo "couldn't get the size<br>";
  42.                     // Use unlink() function to delete a file  
  43.                     if (!unlink($name_source)) {  
  44.                         echo ("$name_source cannot be deleted due to an error");  
  45.                     }  
  46.                     else {  
  47.                         echo ("$name_source has been deleted");  
  48.                     }
  49.                 }
  50.                
  51.             }
  52.                
  53.         }
  54.     }
  55.  
  56. }
  57. ?>
RAW Paste Data Copied