Asked  2 Years ago    Answers:  5   Viewed   145 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?



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.

Monday, September 12, 2022

you need Reflection

if(count($args) == 0)
   $obj = new $className;
else {
   $r = new ReflectionClass($className);
   $obj = $r->newInstanceArgs($args);
Tuesday, August 2, 2022

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:// 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.

Monday, September 12, 2022

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.

Sunday, September 4, 2022

FTPS is FTP using the SSL/TLS protocol for encryption. This is different from the SCP/SFTP family of protocols which use SSH as their transport tunnel.

You will usually use client programs like WinSCP for SCP and SFTP (SFTP is an upgraded version of SCP), whereas you would usually use a web browser or web Download manager (like Filezilla) for FTPS.

FTPS is web-based, using a text command syntax and semantics, whereas SFTP is based on the secure shell protocols common on *NIX systems.

This article describes the differences nicely:

Edit: To expound as requested:

FTP is an Internet protocol that goes back to 1971 (with a number of updates over the years). It is primarily focused on content distribution and in the case of FTPS, uses SSL to provide protection from eavesdropping and illicit modification. FTP authentication requires SSL, to protect credentials in transit.

Most browsers require an external plugin to utilize FTPS, in part because it is no longer a commonly used protocol. When it was popular, web browsers were almost exclusively HTTP only, and everyone used a separate FTP client. At the time FTP/FTPS made the most sense, HTTP was not capable of handling file downloads well. It wasn't until HTTP1.1 was published in 1997 (and updated in 1999) that HTTP could properly handle binary streams.

It is notable that HTTP and FTP use almost identical semantics and syntax, and even use the same well known return codes (200, 301, 403, 404, 500, etc). FTP is somewhat older (HTTP was standardized in 1989 after TCPIP became dominant). The significant similarities between the two protocols indicate a shared lineage, or at least a set of shared conventions. I think that is more than sufficient reason to call FTP "Web-Based", alongside other core application layer protocols like HTTP, SMTP, and DNS (though DNS uses a binary layout, not Text like HTTP/FTP/SMTP).

The SSH family is a set of protocols focused on server administration and remote access to the servers processing capability, rather than simple content distribution. It allows privileged users of a system to connect to a shell to perform work on the server itself. SCP and SFTP were developed to support the many file management tasks related to that work. Imagine you need to install an update to your Apache instance on a remote server. you ssh in, use scpsftp to upload the update package, and then execute it to install. Quite a different work flow than you can achieve with FTPS.

I hope that helps clarify.

Sunday, November 27, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages