Hi, How to copy file from one server to other Remote Server with sftp with PHP-Curl code? I could move the file with normal ftp, using curl functions. But how to achieve it with sftp? What are basic requirements for it? Is it needed that both the server should have SSH access? Please guide, any help will be appreciated. Thanks, Smruti.
You need to install the PECL ssh2 extension - http://pecl.php.net/package/ssh2. Here's how I am doing it on one of my sites. This is assuming that you have a file on the server that you are uploading. Also this is sftp, not ftps, just in case there is some confusion... $src = 'Some_file_location'; $filename = 'new_file.txt'; $dest = '/destination_directory/'.$filename; // set up sftp ssh-sftp connection $connection = ssh2_connect('ftp.otherdomain.com', 22); ssh2_auth_password($connection, 'username', 'password'); // Create SFTP session $sftp = ssh2_sftp($connection); $sftpStream = @fopen('ssh2.sftp://'.$sftp.$dest, 'w'); try { if (!$sftpStream) { throw new Exception("Could not open remote file: $dest"); } $data_to_send = @file_get_contents($src); if ($data_to_send === false) { throw new Exception("Could not open local file: $src."); } if (@fwrite($sftpStream, $data_to_send) === false) { throw new Exception("Could not send data from file: $src."); } else { //Upload was successful, post-upload actions go here... } fclose($sftpStream); } catch (Exception $e) { error_log('Exception: ' . $e->getMessage()); fclose($sftpStream); } PHP: