I know this may seem like a dumb question, but is there a way to get more information about WHY ftp_connect or ftp_login failed? I have this code which works on 2/3 servers I can upload it to (as well as both of my local installs: WAMP, and LAMP) however I can't seem to find anywhere that tells me how to get more information about why its failing on the one. Using phpinfo() I see that FTP is enabled on all servers.

<?php

include('ftp_config.php');

if( $ftp = ftp_connect(FTPHOST) ) {
   if( ftp_login($ftp,FTPUSER,FTPPASS) ) {
      ftp_pasv($ftp,TRUE); // Added based on searching
      if( ftp_put($ftp,'index.php','index.php') ) {
         echo 'File upload successful';
      } else {
         echo 'File upload failed';
   } else {
      echo 'Login failed';
   }
   ftp_close($ftp);
} else {
   echo 'Connection failed';
}

However as would be expected the only information I get on the server that's failing is 'Connection failed'. Looking at the manual for ftp_connect and ftp_login I see no way to get more information about WHY its failing, especially considering the same code and login credentials are working on another server.

Thanks in advance.

    Okay, it's late, but an idea that occurs to me would be to use a network traffic monitor like Wireshark to look at the connection attempt. (I mention Wireshark because it's the one I'm most familiar with, and can give a nicely parsed description of just what got sent in which direction:

    220 dimacs.rutgers.edu FTP server (SunOS 5.8) ready.
    USER anonymous
    331 Guest login ok, send ident as password.
    PASS mozilla@example.com
    230 Guest login ok, access restrictions apply.
    SYST
    215 UNIX Type: L8 Version: SUNOS
    PWD
    257 "/" is current directory.
    TYPE I
    200 Type set to I.
    PASV
    227 Entering Passive Mode (128,6,75,16,200,215)
    CWD /pub/
    250 CWD command successful.
    LIST
    150 Binary data connection for /bin/ls (232.152.215.181,60475) (0 bytes).
    226 Binary Transfer complete.
    

    Between that and RFC 959 to find out what the response codes mean ("Service ready for new user", "User name okay, need password", "User logged in, proceed", "NAME system type.", etc., etc.) you'd at least be able to watch it fall over.

    On a side note, while it won't help with connection problems, for the task your code illustrates the ftp:// stream wrapper would be more concise.

      Thanks weed. Installed wire shark on the server being connected to (aka the ftp server) and noted that the failing site isn't even getting a request to the server at all. So upon further investigation it turns out this host doesn't allow outgoing FTP connections, which begs the question: why have the extension enabled at all?

      The point of the script was it actually works with other parts. One creates a mysqldump file of the database for the site, and the other puts all the files for the site (uploads, scripts, etc.) into a zip file. The finally it uploads that zip for a local FTP server, which gets hard copy back ups made weekly (which would run after the zips were uploaded). The whole purpose was for the customer, and of course theirs is the one server it doesn't work on. So I will be installing PHP on their machine to receive a get request, something like example.com/backup.php?zip=zipname.zip then backup.php will ftp to the server and download it, this seems to work but it seems like an unnecessary step that I'm stuck with.

      I tried the ftp stream wrapper that you suggested, with the same results works on all but this one customers server. Thanks at least I knew where to look for the problem by using Wireshark =D

        Write a Reply...