SFTP from within PHP
Asked Answered
V

5

15

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 and it's hosted on a Linux server.

What I was wondering is whether or not it would be possible to also provide support for SFTP servers, but after some quick searches on Google it seems that this is not all that simple.

So, the question is: What would be the best way to use SFTP from within PHP? Is there a class that could also provide support for FTP as well as SFTP so that same functions could be used for both?

Villain answered 4/4, 2009 at 21:31 Comment(0)
M
14

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.

Megrims answered 4/4, 2009 at 21:36 Comment(1)
Actually you don't even need to mess with CURLOPT_PROTOCOLS. You can just put sftp:// at the front of the hostname you pass with CURLOPT_URL and cURL will automatically detect and use SFTP.Bimetallic
G
7

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

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

Geminius answered 1/7, 2009 at 17:13 Comment(0)
A
4

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
Abra answered 4/1, 2011 at 10:36 Comment(0)
F
3

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

Flita answered 9/1, 2011 at 0:34 Comment(0)
F
1
$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";
    }
}
Fullblown answered 4/2, 2019 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.