0

I am attempting to retrieve a file in a certain directory on Server A from a browser session on Server B using PHP's ftp_connect() function.

Server B's IP address has been whitelisted on Server A.

When I run the script I get "Could not connect to 123.123.123.123".

Here is my script:

<?php
$ftp_host = "123.123.123.123"; // real IP Address masked
$ftp_port = "22";
$ftp_conn = ftp_connect($ftp_host,$ftp_port) or die("Could not connect to $ftp_host");
echo 'connection success'; exit;

Why won't this work? Is there a better way to retrieve the file using PHP?

Here is the results of the "duplicate" question and answers -- they do not work:

  1. $connection = ssh2_connect('123.123.123.123', 22); Results in: Fatal error: Call to undefined function ssh2_connect() in ...
  2. file_get_contents('ssh2.sftp://user:pass@example.com:22/path/to/filename'); Results in Warning: file_get_contents(): Unable to find the wrapper "ssh2.sftp" - did you forget to enable it when you configured PHP?

I am wondering if anyone has had any success making an FTP connection to a server using PHP when you have a proven & valid username, password, host and port.

If you have, can you share how you did it?

10/3/2018 recap and explanation of my objective Here is what I need to do:

  1. There is a file on a host server that is being updated and served to me each and every day of the week by a host datacenter.
  2. The host data center has whitelisted my server's IP address.
  3. I have successfully logged into the host datacenter server via Sublime Text 3 with settings type=sftp, host=(host datacenter's server IP address), port=22, username=(username they gave me), password=(password they gave me), remote_path=(path they gave me.)
  4. I do not want to manually login to the server and physically extract the data file for use on my site -- I want to programmatically do it via PHP (or even bash as someone suggested).

I am open to any assistance or ideas where this has been done successfully.

Problem Solved!

In order to use the ssh2_connect() function, you need to install and activate the PHP SSH2 extension. From all of my digging, googling and research nowhere was it stated that the extension is not active with PHP (out-of-the-box) nor did anyone mention it needed to be installed and activated. But now we know :) I hope this helps you out!

sme1
  • 1
  • 2
  • FYI: FTP establishes _two_ connections between client and server, one on port 22 for commands, and a second on an arbitrary port for data. If you haven't poked the correct holes in your FW for the data connection, or there's a NAT gateway involved, you're not going to be able to connect. – Sammitch Oct 02 '18 at 21:05
  • FTP is not on port 22. – miken32 Oct 02 '18 at 21:07
  • Port 22 is for SSH/SFTP, not FTP. – Martin Prikryl Oct 02 '18 at 21:13
  • Thank you for responding @MichaelBerkowski. Yes, the peculiar thing is that I can FTP from my Sublime Text IDE to the server with no problem at all. And, in fact, the IP that I FTP from in Sublime is my ISP's and it is not the one that the host datacenter whitelisted. As for the rest of the comment, what specially might I ask the hosts datacenter? – sme1 Oct 02 '18 at 22:35
  • Thank you @Sammitch. The host datacenter specifically instructed me to use port 22 which I did in Sublime Text 3 and can connect. My PHP script does not connect using port 22 or even trying port 21. – sme1 Oct 02 '18 at 22:42
  • Let me know what "duplicate" question holds the answer to this problem @MartinPrikryl. Thank you! I look forward to the answer and getting this to work. Best regards. – sme1 Oct 02 '18 at 22:43
  • you should probably use something like bash, PHP is not ideal for this – meda Oct 02 '18 at 22:56
  • Thanks @meda. Have you ever done it in bash? What does the code look like to do it in bash? And when you say "PHP is not ideal for this" does that mean it is impossible to do and PHP cannot do it? Why does PHP have functions like ftp_connect(), and so on? – sme1 Oct 02 '18 at 22:59
  • haha there you have it, wrong port altogether. :P – Sammitch Oct 02 '18 at 23:42
  • If it does not work on port 22 or port 21 @Sammitch what is the right port? The host datacenter told me to use port 22. Port 22 works in Sublime Text. – sme1 Oct 03 '18 at 01:39
  • 1
    You want to connect to port 22 => hence you need to use SFTP, not FTP. So you cannot use `ftp_connect`. You need to use SFTP functions. The answers to the the linked duplicate question show how. – Martin Prikryl Oct 03 '18 at 05:27
  • @meda I do not see why the OP should call a shell for this from a PHP script, if PHP has a native support for SFTP. – Martin Prikryl Oct 03 '18 at 05:36
  • I amended my OQ yesterday after trying the "duplicate" question's suggested answers @MartinPrikryl. They do not work. – sme1 Oct 03 '18 at 09:20
  • 1
    First, you need to find out if you want FTP or SFTP. Stating that SFTP functions do not work for you, still still insisting on FTP makes your question even more confusing than it was before. – Martin Prikryl Oct 03 '18 at 09:23
  • I will use whichever method works @MartinPrikryl in order to retrieve the data file programatically. I amended my OQ another time to restate my objectives. I appreciate any practical assistance you or others who have been successful to do this may have to share with me :) Thanks! – sme1 Oct 03 '18 at 09:41
  • So you say "I have successfully logged into the host datacenter server via Sublime Text 3 with settings type=**sftp**" - Yet you keep asking about **FTP** - Those are two completely unrelated protocols. - Try other methods to use SFTP suggested in the duplicate question - particularly phpseclib. Or install PHP SSH2 module. – Martin Prikryl Oct 03 '18 at 11:26
  • Problem solved -- see my OQ amendment. – sme1 Oct 03 '18 at 15:31

0 Answers0