<?php
$localfile = 'test1.txt';
$fp = fopen ($localfile, "r") or die('Cannot open textfile');
$connection = ssh2_connect('sftp_server_path/folder',22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection) or die ("Cannot connect to server");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"sftp_server_path/folder/test1.txt");
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
if(curl_exec($ch)){
echo 'File was successfully transferred using SFTP';
}else{
echo 'File was unable to be transferred using SFTP';
}
?>
I have the above code to connect and transfer to sftp server and i run this code i am getting message “File was successfully transferred using SFTP”. but when i see the folder structure in sftp server i can’t see my transfer file on sftp server. what exactly i have to do?
Try the following:
https://www.php.net/manual/en/function.ssh2-scp-send.php