3

I need to connect to a remote system to get some files. The specs of the remote system mention that:

The Protocol required is SFTP - SSH File Transfer Protocol The Port Number is 22

Before anything else, I used my FTP client app to try to log into the new server using the given credentials.

At first I (following the system's specs) used a SFTP over SSH connection type and indeed it worked fine.

enter image description here

I researched how one can access a remote resource using SFTP over SSH in PHP, and I got the ssh2 set of commands... But my server doesn't have ssh2 commands installed, nor am I familiar with how to further deal with that in terms of listing and downloading remote files, etc (I'm used to plain ftp_connect(), ftp_login() and ftp_nlist() commands)...

So I tried other options in my FTP client app, and found that despite the new system's specs, it connected fine using FTP using explicit SSL (Auth SSL) and FTP using explicit SSL (Auth TLS) connection types also (the only difference is that in those cases I got a popup asking me to accept and store the remote certificate. Of course I got a list of the remote files also...

So, I researched the implementation of FTP connection using explicit SSL in PHP and got the ftp_ssl_connect() command which is a direct replacement to ftp_connect(). So I thought that anything else could stay the same in my code... But to my disappointment I couldn't get a remote file list using ftp_nlist() command (I'm getting a boolean false instead).

So let me give you the list of commands I used, and the outcome presented in the console:

$conn = ftp_ssl_connect( $ftp_address );
$login = ftp_login( $conn, $ftp_username, $ftp_password );
$files = ftp_nlist( $conn, '.' );

var_dump( $conn );
var_dump( $login );
var_dump( $files );

which returned

resource(4250) of type (FTP Buffer)
bool(true)
bool(false)

I also tried using passive mode before ftp_nlist(), like this:

$conn = ftp_ssl_connect( $ftp_address );
$login = ftp_login( $conn, $ftp_username, $ftp_password );
ftp_pasv( $conn, true );
$files = ftp_nlist( $conn, '.' );

var_dump( $conn );
var_dump( $login );
var_dump( $files );

which returned a PHP Warning: ftp_nlist(): php_connect_nonb() failed: Permission denied (13) in

Can someone give me an idea about why ftp_nlist() fails while the connection itself succeeds, and also give me a suggestion about how to deal with this? TIA!

Faye D.
  • 833
  • 1
  • 3
  • 16
  • Try `FTP_USEPASVADDRESS` along with ftp_pasv, see https://stackoverflow.com/q/40720260/850848 + Can any commandline/GUI FTP client (running the machine where you PHP code fails) can list files on that FTP server? – Martin Prikryl Apr 09 '23 at 07:15
  • According to the docs, `ftp_ssl_connect` is used for "FTP with explicit SSH" and not what you're looking for. FTP over SSH is somewhat complicated, you'll need to establish the SSH connection separately and operate 2 channels (data channel is SSH, the control channel is unsecured)- Nowadays you should be using SFTP. Here's a comparsion of different techniques https://www.awardspace.com/kb/what-is-ftp-over-ssh/#ftp-over-ssh-vs-sftp – Honk der Hase Apr 09 '23 at 09:22
  • @HonkderHase You've got in a muddle between SSL (also known as TLS) and SSH. There's no mention of the incredibly rare "FTP over SSH" in the question; they were told to use "SFTP - SSH File Transfer Protocol", but also seemed able to connect with "FTP using explicit SSL", which is exactly what [ftp_ssl_connect](https://www.php.net/manual/en/function.ftp-ssl-connect.php) implements. – IMSoP Apr 09 '23 at 10:32
  • @IMSoPto be exact I mentioned that through a GUI FTP client I can connect and list files using both types, SFTP over SSH and FTP over Explicit SSL (Auth TLS). I only chose to use ftp_ssl_connect over ssh2 because ssh2 isn't installed on the server + I'm not familiar with it! – Faye D. Apr 09 '23 at 10:38
  • @MartinPrikrylyes, using the FTP Client I can list files successfully using FTP over SSL! – Faye D. Apr 09 '23 at 10:39
  • But do you run the FTP client on the *same machine* that runs your PHP code? + Did you try the `FTP_USEPASVADDRESS`? – Martin Prikryl Apr 09 '23 at 15:50
  • @MartinPrikryl no, the machine that runs the code is a linux webserver, the app ran from my local machine. In the code I tried the option FTP_USEPASVADDRESS before ftp_pasv(). Nothing changed! – Faye D. Apr 09 '23 at 19:24
  • @MartinPrikryl I just used my code in a totally different server on a shared hosting I have on HG, and it does the exact same thing. `var_dump($ftpConn); var_dump($login); var_dump($files_array);` return `resource(4) of type (FTP Buffer) bool(true) bool(false)` respectively... – Faye D. Apr 09 '23 at 21:08
  • @MartinPrikryl would you care to help me a bit more with this? If yes, let me know so that I can PM you the connection info! This is the rest of the code I used: ` – Faye D. Apr 09 '23 at 21:28
  • @MartinPrikryl BTW, even with `FTP_USEPASVADDRESS` option in place, and `ftp_pasv`, I still get a warning mentioning: _Warning: ftp_nlist(): php_connect_nonb() failed: Operation now in progress (115)_ – Faye D. Apr 09 '23 at 21:40
  • @MartinPrikryl I achieved what I needed with cURL, but TBH I'd like to see this working for the shake of knowledge, so if you wanna get involved in debugging this, do let me know! – Faye D. Apr 10 '23 at 02:32

0 Answers0