Advertisement
erptarragona

download sql files

Jun 13th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2. $host = 'sftp.example.com';
  3. $port = 22;
  4. $username = 'sftp-user';
  5. $password = 'abc123456789';
  6. $remoteDir = '/filesDir/';
  7. $localDir = '/files/';
  8.  
  9. if (!function_exists("ssh2_connect"))
  10.     die('Function ssh2_connect does not exist.');
  11.  
  12. if (!$connection = ssh2_connect($host, $port))
  13.     die('Failed to connect.');
  14.  
  15. if (!ssh2_auth_password($connection, $username, $password))
  16.     die('Failed to authenticate.');
  17.  
  18. if (!$sftp_conn = ssh2_sftp($connection))
  19.     die('Failed to create a sftp connection.');
  20.  
  21. if (!$dir = opendir("ssh2.sftp://$sftp_conn$remoteDir"))
  22.     die('Failed to open the directory.');
  23.  
  24. $files = array();
  25. while ( ($file = readdir($dir)) !== false)
  26. {
  27.     if(substr($file, -4)==".sql")
  28.     {
  29.         $files[]=$file;
  30.     }
  31. }
  32. closedir($dir);
  33.  
  34. foreach ($files as $file)
  35. {
  36.     echo "Copying file: $file\n";
  37.     if (!$remote = fopen("ssh2.sftp://$sftp_conn$remoteDir$file", 'r'))
  38.     {
  39.         echo "Failed to open remote file: $file\n";
  40.         continue;
  41.     }
  42.  
  43.     if (!$local = fopen($localDir . $file, 'w'))
  44.     {
  45.         echo "Failed to create local file: $file\n";
  46.         continue;
  47.     }
  48.  
  49.     $read = 0;
  50.     $filesize = filesize("ssh2.sftp://$sftp_conn/$remoteDir$file");
  51.     while ( ($read < $filesize) && ($buffer = fread($remote, $filesize - $read)) )
  52.     {
  53.         $read += strlen($buffer);
  54.         if (fwrite($local, $buffer) === FALSE)
  55.         {
  56.             echo "Failed to write to local file: $file\n";
  57.             break;
  58.         }
  59.     }
  60.     fclose($local);
  61.     fclose($remote);
  62. }
  63. ?>
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement