Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFTP from within PHP

Tags:

php

curl

sftp

ftp

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?

like image 738
titel Avatar asked Apr 04 '09 21:04

titel


People also ask

How to connect to SFTP server in PHP?

Example #1 Opening a file via SFTP$connection = ssh2_connect('shell.example.com', 22); ssh2_auth_password($connection, 'username', 'password'); $sftp = ssh2_sftp($connection);

How to upload file to SFTP server using PHP?

php'); //connection parameters $host = 'host_url'; $port = 'host_port_number'; $username = 'host_user_name'; $password = 'host_password'; //Connect to SFTP host $sftp = new Net_SFTP($host, $port); if ( $sftp->login($username, $password) ) { //create file on remote server directory and with some text in it $success = $ ...

How do I manually connect to SFTP?

How to Connect to SFTP. By default, the same SSH protocol is used to authenticate and establish an SFTP connection. To start an SFTP session, enter the username and remote hostname or IP address at the command prompt. Once authentication is successful, you will see a shell with an sftp> prompt.


2 Answers

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.

like image 125
vartec Avatar answered Nov 04 '22 03:11

vartec


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

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

like image 28
dreamafter Avatar answered Nov 04 '22 02:11

dreamafter