Asked  7 Months ago    Answers:  5   Viewed   39 times

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?

 Answers

34

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.

Wednesday, March 31, 2021
 
Litty
answered 7 Months ago
81

you need Reflection http://php.net/manual/en/class.reflectionclass.php

if(count($args) == 0)
   $obj = new $className;
else {
   $r = new ReflectionClass($className);
   $obj = $r->newInstanceArgs($args);
}
Wednesday, March 31, 2021
 
Hexaholic
answered 7 Months ago
62

The curl obviously tries to use an implicit FTP (as it initializes TLS/SSL session even before any FTP commands are exchanged with the server).

It's because you have specified the ftps:// prefix, which is used for the implicit TLS. It has the special prefix, because the implicit TLS uses a special port (990). But you overrode the default with CURLOPT_PORT.

While the explicit TLS uses the standard FTP port (21), so it uses the standard ftp:// prefix. To enable the explicit TLS, use the CURLOPT_USE_SSL (what you are doing already, just via a wrong value, the option type is enumeration, not boolean).

So the code should be:

$server_data = array(
   ...
   'post_url' => "ftp://ftps.widgetsltd.com", // ftp:// URL
   ...
);

curl_setopt($ch, CURLOPT_URL, sprintf("%s/%s", $server_data['post_url'], $filename));
curl_setopt($ch, CURLOPT_USE_SSL, CURLUSESSL_ALL); // Enable TLS/SSL

Note that the CURLOPT_FTP_SSL is obsoleted and is an alias to the CURLOPT_USE_SSL. So, it does not make sense to set both.

Saturday, May 29, 2021
 
Oshrib
answered 5 Months ago
40

You can simply write a call to the sftp command line client via a batch file if you want to accomplish that.

Check out the Shell() function in VBA.

Under the click event of the button on your form add in the code:

mySFTPCall = "sftp <insert your options here!>"
Call Shell(mySFTPCall, 1)

I've used this before to just copy files straight across network shares etc. to share data from an in-house Access DB. Of course you could get more fancy if necessary.

Saturday, August 14, 2021
 
williamcarswell
answered 3 Months ago
88

The error message is actually very relevant.

It's Pure-FTPd error response to PORT command, when the client is asking the server to open a data connection to an IP address different to that of the client (as the server sees it). So it's a remote error message sent by the server, not a local error message produced by PHP.

The problem is typically caused by the client knowing only its local IP address, not the external IP address.


Now, the question is why PHP uses PORT, when you asked it to connect using passive mode (ftp_pasv).

It's clearly because you call ftp_pasv before even logging in.

So the server rejects the PASV call with:

530 You aren't logged in

Unfortunately the PHP does not propagate error messages from PASV call. And it silently falls back to the default active mode. You can tell the call failed by its return value (that you do not check).


Just move the ftp_pasv call after the ftp_login.

$conn_id = ftp_connect('50.xx.xx.xx');

ftp_login($conn_id, 'user', 'pass');

ftp_pasv($conn_id, true);

The documentation clearly suggests it:

Please note that ftp_pasv() can only be called after a successful login or otherwise it will fail.


And of course, you should do a better error checking.


For a similar issue (just with ProFTPd), see:
PHP ftp_put returning "Unable to build data connection: Connection refused"

Thursday, September 30, 2021
 
Jing
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :