SFTP Upload to remote server

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.