Link to home
Start Free TrialLog in
Avatar of elysiandc
elysiandc

asked on

PHP & FTP: ftp_connect error - php_network_getaddresses: getaddrinfo failed: No such host is known

Hi all,

I'm a PHP noob, so please excuse my ignorance....

I have the following script;
<?php

// define some variables
$ftp_server= "ftp://ftp2.bom.gov.au/anon/gen/fwo";
$ftp_user_name= "anonymous";
$ftp_user_pass= "webmaster@aquaticadventures.com.au";
$local_file = "4day.txt";
$server_file = "IDV10450.txt";


// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!<br>";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name<br>";
        die;
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name<br>";
    }

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
   echo "Successfully written to $local_file\n";
} else {
   echo "There was a problem\n";
}

// close the connection
ftp_close($conn_id);

?>

Which gives the following error;
Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\Elysian Visions\AA test page\4day.php on line 12

Warning: ftp_login() expects parameter 1 to be resource, boolean given in D:\Elysian Visions\AA test page\4day.php on line 15
FTP connection has failed!
Attempted to connect to ftp://ftp.bom.gov.au for user anonymous

I'm not sure why it won't connect to the FTP server, which is a public server provided by the Bureau of Meteorology (Government), hence the user & password.

Do I have to do something in php.ini to allow FTP via PHP? Or do I need to have an IP address to connect to instead of ftp://ftp2.bom.gov.au/anon/gen/fwo? Or do I have a stupid syntax error I can't seem to spot?

Thanks a million for your help.

The BFG :D
Avatar of ch2
ch2

change this:

$ftp_server= "ftp://ftp2.bom.gov.au/anon/gen/fwo";

for

$ftp_server= "ftp2.bom.gov.au";

and this:

$server_file = "IDV10450.txt";

for

this:

$server_file = "/anon/gen/fwo/IDV10450.txt";
Avatar of elysiandc

ASKER

Hey Ch2,

Thanks for the quick reply - I gave that a shot and get the same errors...

Any other tips?

Cheers

BFG
ASKER CERTIFIED SOLUTION
Avatar of ch2
ch2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It worked! You're a chanpion!

Thanks a million.  :D
Can I ask where you got the IP from? Also wondering - does this mean that for $ftp_server you can't use a URL, only an IP addrress?

Again, thanks for your help.

The BFG
<<Can I ask where you got the IP from?

well i just resolved the domain you posted.

An easy way to do resolve a domain is to ping the domain name with the ping tool wich comes with windows xp and other operating systems as well.

<<Also wondering - does this mean that for $ftp_server you can't use a URL, only an IP addrress?

You can use an ip address or a domain, both works.

If using a domain you must resolve the domain fisrt. To do it in PHP use gethostbyname()

gethostbyname() could be other option to resolve your issue.