Hi all,

I am trying to connect to our FTP server with the following script.

I already have access via our FTP client (WSFTPpro) which works fine.

When I use the PHP script (see below) I get the following error:

Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/vhosts/mydomain/httpdocs/fidsdata/ftptest2.php on line 9

Warning: ftp_login() expects parameter 1 to be resource, boolean given in /var/www/vhosts/mydomain/httpdocs/fidsdata/ftptest2.php on line 16
Ftp connection has failed!Attempted to connect to [url]ftp://myserver.com/[/url] for user myuser.

$ftp_user_name = 'myuser';
$ftp_user_pass = 'mypassword';


// set up basic connection
$ftp_server = 'ftp://mydomain.com/'; 
$ftp_port = '21'; 
$conn_id = ftp_connect($ftp_server,$ftp_port); 


// 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!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
die; 
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
echo "Ftp upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream 
ftp_close($conn_id); 

Can anyone see where I am oing wrong.

    While I hate giving simple "RTFM"-type answers, I must admit that doing so would solve your (present) problem:

    [man]ftp_connect/man

    PHP manual wrote:

    host
    The FTP server address. This parameter shouldn't have any trailing slashes and shouldn't be prefixed with [url]ftp://.[/url]

      Hi Brad and thank you for your reply.

      I totally agree that reading the manual is the first stop. The problem I have here is, not only have I read the manual I endded up having it for lunch. I have gone backward and forward with this problem and tried just about every possible tweek I know to get it to work but sadly without a result.

      If you can see anything I have missed it would be helpful. I am currently working with:

      $ftp_user_name = 'myuser';
      $ftp_user_pass = 'mypassword';
      
      
      // set up basic connection
      $ftp_server = 'ftp://mydomain.com';
      $ftp_port = '21';
      $conn_id = ftp_connect($ftp_server,$ftp_port);
      
      
      // 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!";
      echo "Attempted to connect to $ftp_server for user $ftp_user_name";
      die;
      } else {
      echo "Connected to $ftp_server, for user $ftp_user_name";
      }
      
      // upload the file
      $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
      
      // check upload status
      if (!$upload) {
      echo "Ftp upload has failed!";
      } else {
      echo "Uploaded $source_file to $ftp_server as $destination_file";
      }
      
      // close the FTP stream
      ftp_close($conn_id); 
      

      Many thanks again for your time.

        I would expect PHP to change '21' into an integer in ftp_connect()
        but who knows?
        int means integer and '21' is a string

        ftp_connect ( string $host [, int $port = 21

          dcjones wrote:

          If you can see anything I have missed it would be helpful. I am currently working with:

          Yes; read the formatting requirements for the host name again.

            Hi All and thanks for all your time.

            I have changed the script as per the manual but I am still getting the following error;

            Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /var/www/vhosts/mydomain/httpdocs/ftptest2.php on line 12

            I can connect using an FTP Client or a browser with no error and for the life of me I can't see why it will not with the script changes. Any ideas as to why.

            This is the code now:

            $ftp_user_name = 'myuser';
            $ftp_user_pass = 'mypassword';
            
            
            // set up basic connection
            $ftp_server = 'targetdomain.com';
            
            $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!";
            echo "Attempted to connect to $ftp_server for user $ftp_user_name";
            die;
            } else {
            echo "Connected to $ftp_server, for user $ftp_user_name";
            }
            
            // upload the file
            $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
            
            // check upload status
            if (!$upload) {
            echo "Ftp upload has failed!";
            } else {
            echo "Uploaded $source_file to $ftp_server as $destination_file";
            }
            
            // close the FTP stream
            ftp_close($conn_id); 
            

              "Temporary failure in name resolution" sounds like it was a DNS issue, as in the server couldn't connect to a DNS server that would resolve the "targetdomain.com" host into an IP address. This could be an error, or it could be a result of the administrator of the server (e.g. your web host) blocking connections to external servers.

              If you know that the host name is correct (e.g. you can connect to it yourself, as you've said), then I would contact your host and tell them what you're trying to do (use a PHP script to connect to an external FTP server), copy/paste the error message, and ask them if this is a problem with the server OR if they don't allow this type of thing.

                I think this is an error you can get when
                allow_url_fopen is set to OFF
                But probably can be some other thing, that causes this same error.

                This may be a solution or not, but ....
                If you run phpinfo(); you can see value of allow_url_fopen
                If it is set to OFF you might be able to turn it ON.
                Put this line in your .htaccess

                php_flag allow_url_fopen On

                The same config can of course be set in php.ini if you have access to it.

                  halojoy;10950932 wrote:

                  I think this is an error you can get when
                  allow_url_fopen is set to OFF

                  That would have nothing to do with the ftp extension, since no URL wrappers are being used at all (plus, the error message would state that an allow_url_fopen restriction is in place).

                  EDIT: In addition, even if the above directive were the culprit, you'd only be able to change it in php.ini - not a .htaccess file.

                    Write a Reply...