Cannot connect with FTP server with PHP , ftp_connect() - php

I was trying to connect with ftp server using ftp_connect() function of PHP as shown below:
<?php
$ftp_server = "http://ftp.mozilla.org/pub/mozilla.org/";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
?>
But it returns this error:
Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\wamp\www\ftp2.php on line 6
Although this is a very common type of error, I still cannot find any solution. Can anyone provide some possible solutions?
Thank you for your time.

You must supply only the ftp server hostname, rather than the hostname and directory path, and the irrelevant http:// since this is an FTP connection.
$ftp_server = "ftp.mozilla.org";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// Then chdir to the correct directory:
ftp_chdir($conn_id, "/pub/mozilla.org");
See the full documentation of PHP's FTP functions.

Get rid of the http://, it is not part of the server address.

Related

PHP Warning: ftp_login(): Login with USER first

I am trying to connect and log in to my FTP server. I connect fine but when i login, ftp_login() returns false and a warning like this:
PHP Warning: ftp_login(): Login with USER first.
My code is below.
$ftp_conn = ftp_ssl_connect($ftp_server) or die("Could not connect to $ftp_server");
echo "Connected to $ftp_server\n";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
var_dump($login);
var_dump(error_get_last());
echo "Logged in to $ftp_server\n";
Tried to search for answers but not much information on the net.
Please help.
Thank you.
It was a firewall issue. Make sure your FTP Server's firewall allows inbound connection on the port you are trying to connect to. In my case, port 990.
Thanks.

PHP FTP connecting but not reading folder

I have a code I am trying to connect to a different server via PHP FTP connection.
I know that I am actually connecting to the server.
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
var_dump($login);
when I vardump the $login, I get a TRUE.
When I try to upload a file I get a 'error uploading file'
so I tried to just pull a list of files on the connection:
$file_list = ftp_nlist($ftp_conn, ".");
var_dump($file_list);
It's only returning bool(false).
I know the connections has files because I can view them via FileZilla with the same credentials.
Any idea what might be wrong? Is it possible a server setting that isn't allowing me to use this PHP script from a shared server?
Most typical cause of problems with ftp_list (ftp_nlist, ftp_put and other functions that require separate FTP data connection) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the directory listing and transfer working. Use the ftp_pasv function.
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass) or die("Login failed");
// turn passive mode on
ftp_pasv($ftp_conn, true) or die("Unable switch to passive mode");
See my article on the active and passive FTP connection modes.

Warning: ftp_put(): Connecting to port

I cannot upload files into my ftp server.
There is always warning:
ftp_put(): Connecting to port.
<?php
set_time_limit(0);
$host = 'xxxx';
$usr = 'yyyy';
$pwd = 'zzzz';
$local_file = '/home/back.sql';
$ftp_path = '/public_html/';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path.'back1.sql', $local_file, FTP_ASCII);
print($upload);
?>
The code were executed for three times. I got three different warnings.
Warning: ftp_put(): Connecting to port 1926 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1928 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1930 in filename (i named it) on line 10
What does the warning info mean?
Why connect to different ports? Maybe the ports should be 21 for every time, why not?
The "Connecting to port xxx" is a message issued by PureFTPD server, when it tries to connect back to the FTP client to its active mode data connection port (which is random, that's why it changes).
If you really need to use the active mode, you need to allow incoming connections to the active mode data connection port range used by PHP.
See my guide for network configuration necessary for the active mode FTP.
Though, if you do not need to use the active mode, use the passive mode instead. The passive mode generally does not require any network configuration on a client side.
In PHP, you switch to the passive mode by calling the ftp_pasv function after the ftp_login:
...
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
...
See the above guide to understand the difference between the active and the passive FTP mode.

Error uploading images to server using ftp and php script, giving error on ftp_put() login

I have a script written in php which basically uploads images to a server.
I'm uploading this script onto my server using an ftp account credentials. However when I try to access it, it gives me login error. Here's the error log:
[09-Apr-2014 19:00:01 Asia/Kolkata] PHP Warning: ftp_login(): Sorry,
cleartext sessions are not accepted on this server. in
/home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014
19:01:33 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext
sessions are not accepted on this server. in
/home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014
19:01:55 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext
sessions are not accepted on this server. in
/home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014
19:02:21 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext
sessions are not accepted on this server. in
/home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014
19:03:59 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext
sessions are not accepted on this server. in
/home/gameeon/public_html/jts/local_upload.php on line 8
My code is as follows:
<?php
// connect and login to FTP server
$ftp_server = "ftp.gameeon.in"; // enter the ftp host name
$ftp_username = "temp#gameeon.in"; // put your ftp user name
$ftp_userpass = "********"; // put your ftp password
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
/// put the path of your image folder in your remote server i.e:
$ftp_upload_path="public_html/img/";
/// put the path of your image folder in your local machine:
$files = glob("E:/upload_images/*.*");
foreach($files as $fl)
{
$fl_arr = explode("/",$fl);
$cn=count($fl_arr);
$num=$cn-1;
$file=$fl_arr[$num];
// upload file
// $fl = full path of localimages
if (ftp_put($ftp_conn, $ftp_upload_path.$file,$fl, FTP_ASCII))
{
echo "Successfully uploaded $file. <br/>";
}
else
{
echo "Error uploading $file.<br/>";
}
sleep(3); /// pausing the code for 3 secs before next upload
}
// close connection
ftp_close($ftp_conn);
?>
I'm getting an error on this line no. 8 which is:
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
You need to use ftp_ssl_connect() function instead of ftp_connect() because server is using Explicit TLS/SSL
Try this:
$ftp_conn = ftp_ssl_connect($ftp_server)
or die("Could not connect to $ftp_server");
If you are working on localhost you may encounter error saying
ftp_put(): I won't open a connection to 192...* something..
Then you have to use the ftp_pasv function.
As of docs:
In passive mode, data connections are initiated by the client, rather
than by the server. It may be needed if the client is behind firewall.
In your case yes you need to use it you are using Explicit TLS/SSL just before your loop add this:
ftp_pasv($ftp_conn, true);
Also set your path public_html/img/ to /img/ or whatever directory exists:
$ftp_upload_path = "/img/";

Can't connect to my FTP server via ftp_connect()

I have some problems with ftp_connect(); I can't connect to my own FTP-server via ftp.localhost or ftp.edgren.myftp.org. I got this error message when I try to connect:
Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in ...
I use this code: $conn_id = ftp_connect('ftp.localhost') or die("Couldn't connect to localhost");
What's the problem? I use the basic FTP-server program Wing FTP Server to host my server.
Thanks in advance.
Try connecting to 127.0.0.1. Localhost is just an alias to that IP, so if localhost doesn't work, the IP will.

Categories