SFTP from within PHP

前端 未结 5 1389
别那么骄傲
别那么骄傲 2020-11-27 07:17

I\'m in the process of building an web app that will, besides other things, need to connect to a FTP server to download or upload files. The application is written in PHP an

相关标签:
5条回答
  • 2020-11-27 07:28

    Yes, you can do that with cURL. To switch from FTP to SFTP all you have to do is to change protocol option form CURLPROTO_FTP to CURLPROTO_SFTP.

    cURL supports following protocols: HTTP, HTTPS , FTP, FTPS, SCP, SFTP, TELNET, LDAP, LDAPS, DICT, FILE, TFTP.

    BTW. SFTP is not to be confused with FTPS. SFTP is SSH File Transfer Protocol, while FTPS is FTP over SSL.

    0 讨论(0)
  • 2020-11-27 07:31

    In case someone end-up on this page.

    You also may use the PHP Bindings for LIBSSH2 with PHP. It should be appropriately installed on the system.

    In Ubuntu 10.04 and Debian Lenny, of course with all dependences

    apt-get install libssh2-php
    
    0 讨论(0)
  • 2020-11-27 07:33

    if you don't have cURL installed (my host doesn't), you can use phpseclib:

    http://phpseclib.sourceforge.net/documentation/net.html#net_sftp

    0 讨论(0)
  • 2020-11-27 07:42

    The problem with Igor's recommendation is that it, among other things, makes for much less portable code (libssh2 isn't installed on very many hosts), it has a far more intuitive OOP-based API and RSA authentication actually makes sense (libssh2 requires you store the public key and the private key separately on the file system; the fact that they have to be separately provided is silly since most private key formats include the public key within them).

    phpseclib is also faster:

    http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/#comment_3759

    0 讨论(0)
  • 2020-11-27 07:50
    $dataFile      = 'PASTE_FILE_NAME_HERE';
    $sftpServer    = 'PASTE_SFTP_SERVER_NAME_HERE';
    $sftpUsername  = 'PASTE_USERNAME_HERE';
    $sftpPassword  = 'PASTE_PASSWORD_HERE';
    $sftpPort      = 'PASTE_PORT_HERE';
    $sftpRemoteDir = '/';
    
    $ch = curl_init('sftp://' . $sftpServer . ':' . $sftpPort . $sftpRemoteDir . '/' . basename($dataFile));
    
    $fh = fopen($dataFile, 'r');
    
    if ($fh) {
        curl_setopt($ch, CURLOPT_USERPWD, $sftpUsername . ':' . $sftpPassword);
        curl_setopt($ch, CURLOPT_UPLOAD, true);
        curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
        curl_setopt($ch, CURLOPT_INFILE, $fh);
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize($dataFile));
        curl_setopt($ch, CURLOPT_VERBOSE, true);
    
        $verbose = fopen('php://temp', 'w+');
        curl_setopt($ch, CURLOPT_STDERR, $verbose);
    
        $response = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
    
        if ($response) {
            echo "Success";
        } else {
            echo "Failure";
            rewind($verbose);
            $verboseLog = stream_get_contents($verbose);
            echo "Verbose information:\n" . $verboseLog . "\n";
        }
    }
    
    0 讨论(0)
提交回复
热议问题