Php+sftp Upload and download of implementation files

Source: Internet
Author: User
Tags php file upload
This article to share the content is PHP+SFTP implementation of the file upload and download, has a certain reference value, the need for friends can refer to

Recently developed a project to work with the bank, need to upload and download files. There is a certain requirement for file secrecy, so sftp is used. But the actual development, encountered a lot of problems, the online search for the tutorial and cases can not be used, but also times, copy, copy to. Finally in the continuous debugging after the implementation of the PHP file upload and download. is now recorded for informational purposes only.

1. Check the PHP version, download the corresponding SSH2 extension https://windows.php.net/downloads/pecl/releases/ssh2/, specific installation, trouble searching online. Phpinfo () Check if the installation is good. After the installation is successful, you can see the SSH2.

2. upload next Cut method available online (I believe you've seen it)

<? php//php environment must have ssh $strServer = "Server IP";//server IP $strServerPort = "22";//Port number • $strServerUsername = "* * *";//user name $ Strserverpassword = "* * *";//password//connect to server $resConnection = Ssh2_connect ($strServer, $strServerPort); if (Ssh2_auth_password ($resConnection, $strServerUsername, $strServerPassword)) {//initialize sftp $resSFTP = ssh2_sftp ($ resconnection); • echo $resSFTP; · Download file//1 $filename = ' D:\down '. Time (). TXT '; · $opts = Array (* ' http ' =>array (· ') Method ' = ' and ' GET ', • ' Timeout ' =>60,) · $context = Stream_context_create ($opts); $strData = file_get_contents ("ssh2.sftp://{$resSFTP}/var/testfile/abc.txt", false, $context); File_put_contents ($filename, $strData); 2 can also use copy ()//if (!copy ("ssh2.sftp://{$resSFTP}/dfr508/wun/ikea-logo.jpg", $filename)) {//echo ' download failed ' ;· //}·//--------------------------------------------------------------------------------------------------------- -----//Upload file//1//file_put_contents ("ssh2.sftp://{$resSFTP}/var/testfile/456.txt", ' d:/ab.txt '); 2You can also use copy () if (!copy ("D:/ab.txt", "ssh2.sftp://{$resSFTP}/var/testfile/up". Time (). TXT ") {echo ' upload failed ';}} else {echo "cannot authenticate to the server";}? >

But I follow the online tutorial, how also can not achieve, finally had to step by step to troubleshoot the cause.

         $strServer = "";//server IP                  $strServerPort = "";//port number      $strServerUsername = "";//username      $strServerPassword = "";// Password     //1 connection server           $resConnection = Ssh2_connect ($strServer, $strServerPort);             2 Verify             the connection if (Ssh2_auth_password ($resConnection, $strServerUsername, $strServerPassword)) {                 //3 initialize SFTP                      $ Ressftp = Ssh2_sftp ($resConnection);

First 3 steps are no problem.

Use copy, File_get_content, curl method to upload and download all reported 502 error. Check the path and other issues, no problem, but it is not successful.

It is later inferred that the file address on the SFTP server cannot be accessed.

"Ssh2.sftp://{$resSFTP}/var/testfile/abc.txt"

Now it's only possible to find a really usable address, see a lot of data, and finally find it in the PHP manual.

Ssh2_sftp_realpath checks the true path of the file, and finally returns the address of the file accessed after the SFTP connection. Incoming "ssh2.sftp://{$resSFTP}/var/testfile/abc.txt"

Will error, indicating that the address is wrong. Finally check out the upload, the download address does not need to add the previous

sh2.sftp://{$resSFTP}

Directly with the Sftp file path.

The upload function is also not available with the previously mentioned copy, file_get_content, Curl

Need to use

Ssh2_scp_send Upload

SSH2_SCP_RECV Download

Related Article

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.