Development encountered a demand, the need to upload pictures via SFTP to the remote server, before the use of this function, toss me a few genius to fix, the following records my treatment:
$sftp= ' ssh2.sftp://';
Connect SFTP$conn= Ssh2_connect (' IP ', ' Port '));
Login Ssh2_auth_password ($conn, "User", "password");$result= Ssh2_sftp ($conn);
Determine if there is a directory HMif(!file_exists($sftp.$result.‘ /hm/')) {$dir= Ssh2_sftp_mkdir ($result, '/hm/', 0777,true);}Else{$dir=true;}if($dir{//realpath method to convert relative path to absolute path $send = Ssh2_scp_send ($conn, Realpath ($localfile), $remotefile);
The beginning of this method is the test is feasible, then the FTP account permissions are modified, resulting in the root directory, and then do not know why it is not, then only the curve to the salvation of the following methods}Else{$send=false;}
Remote server creates a file
$sftpStreamfopen($sftp. $result. $remotefilename, ' W ');
Get local file $data _to_sendfile_get_contents(realpath($localfilename)) ;
Writes a local file to a remote file $sendfwrite($sftpStream$data _to_send);
Close file stream fclose($sftpStream);
SFTP Upload to remote server