1

I can't get this to connect to the FTP server for some odd reason, can anyone help me? What it does is tell me that it could not connect to the ftp server.

<?php
// connect and login to FTP server
$ftp_server = "serverip";
$ftp_port = "22";
$ftp_username = "vpslogin";
$ftp_userpass = "vpspassword";

$ftp_conn = ftp_connect($ftp_server,$ftp_port) or die("Could Not Connect To FTP Server: "$ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$server_file = "directoy/listing";

// open local file to write to
$local_file = "local.txt";
$fp = fopen($local_file,"w");

// download server file and save it to open local file
if (ftp_fget($ftp_conn, $fp, $server_file, FTP_ASCII, 0))
  {
  echo "Successfully written to $local_file.";
  }
else
  {
  echo "Error downloading $server_file.";
  }

// close connection and file handler
ftp_close($ftp_conn);
fclose($fp);
?>
5
  • 1
    Looks like you are trying to ftp over ssh...port 22 is ssh. Port 21 is FTP Commented Mar 9, 2015 at 4:34
  • @Rafael Port 22 is sftp and SSH, no? Dailyinsanity, If you convert the port from a string to an INT does it work? If not you may need to try php.net/manual/en/function.ftp-ssl-connect.php. Commented Mar 9, 2015 at 4:40
  • 1
    @chris85 That is the wrong advice, SSL is completely different alternative. The OP is confusing ftp_connect with ssh2_connect php.net/manual/en/function.ssh2-connect.php Like I said already, he needs to be on port 21 for his code to work. php.net/manual/en/function.ssh2-sftp.php Commented Mar 9, 2015 at 4:43
  • @Rafael I access the ftp on port 22 (same as the SSH (it's on a debian machine)), but i figured out the problem, it was something to do with the account config on the machine. Commented Mar 9, 2015 at 6:36
  • 1
    @dailyinsanity That's just nonsense. Anyway, please delete your question or post your solution. Commented Mar 9, 2015 at 8:05

1 Answer 1

1

Port 21

you also forgot concat on line 8


<?php

# FTP Info
$ftp_server   = "serverip";
$ftp_username = "vpslogin";
$ftp_userpass = "vpspassword";

$ftp_conn = ftp_connect($ftp_server) or die("Could Not Connect To FTP Server: $ftp_server");

ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$server_file = "directoy/listing";

# Open Writable File
$local_file = "local.txt";
$fp = fopen($local_file,"w");

// download server file and save it to open local file
if (ftp_fget($ftp_conn, $fp, $server_file, FTP_ASCII, 0))
  echo "Successfully written to $local_file.";
else
  echo "Error downloading $server_file.";

# Close all
ftp_close($ftp_conn);
fclose($fp);
Sign up to request clarification or add additional context in comments.

1 Comment

i am trying to connect ftp, but getting error. and error id "Could Not Connect To FTP Server". i am using same code with my conditional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.