ftp_nlist "425 Can't open data connectoin"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • blackpuppy

    ftp_nlist "425 Can't open data connectoin"

    I am just beginner on PHP. I am reading "Spring Into PHP 5" but cannot
    make the ftp examples work.

    I am running PHP scripts on a Fedora Core 5 (VMWare virtual machine) on
    top of a Windows XP Professional. The PHP version is 5.1.4. The PHP
    script is shown below.

    #! /usr/bin/php
    <?php
    $ftp_server = "myftpserve r";
    $connect = ftp_connect($ft p_server)
    or die("Couldn't connect to $ftp_server.");
    $result = ftp_login($conn ect, "myftpuser" , "mypassword ")
    or die("Couldn't log in $ftp_server.");
    $a = ftp_nlist($conn ect, "/");
    foreach($a as $value){
    echo $value, "\n";
    }
    ?>

    I run the above script in a GENOME terminal. It seems it can connect
    and log in to the ftp server successfully. But then the ftp_nlist()
    call fails. And the server has the following log for this call.
    NLST /
    150 Opening data channel for directory list.
    425 Can't open data connection.
    I do not know what is wrong. But in a terminal I can manually log in
    the ftp server, and the ls or nlist command will give the file list as
    expected. So I suspect the problem is at the PHP ftp function.

    The PHP manual on FTP functions
    (http://www.php.net/manual/en/ref.ftp.php) has the following
    explanation.

    "No external libraries are needed to build this extension.

    In order to use FTP functions with your PHP configuration, you should
    add the --enable-ftp option when installing PHP 4 or greater or
    --with-ftp when using PHP 3.

    This extension has no configuration directives defined in php.ini."

    My questions are:
    - How could I know what is wrong with the ftp_nlist() call?
    - How could I know if the FTP functions are enabled at PHP
    installation?
    - How to make the FTP functions work?

    Thanks a lot!

  • Patrick Schlangen

    #2
    Re: ftp_nlist &quot;425 Can't open data connectoin&quot ;

    Hi,
    - How could I know if the FTP functions are enabled at PHP
    installation?
    they are. Otherwise you would get a error about a undefined function.
    - How to make the FTP functions work?
    You might need to use PASSIVE ftp mode (e.g. because of firewall or NAT
    settings).
    Just try this:

    #! /usr/bin/php
    <?php
    $ftp_server = "myftpserve r";
    $connect = ftp_connect($ft p_server)
    or die("Couldn't connect to $ftp_server.");
    $result = ftp_login($conn ect, "myftpuser" , "mypassword ")
    or die("Couldn't log in $ftp_server.");
    ftp_pasv($conne ct, true);
    $a = ftp_nlist($conn ect, "/");
    foreach($a as $value){
    echo $value, "\n";
    }
    ?>

    I just added one line (""ftp_pasv($co nnect, true);"").
    Please let us know if it works now.

    Patrick


    Comment

    • blackpuppy

      #3
      Re: ftp_nlist &quot;425 Can't open data connectoin&quot ;

      Thanks, Patrick!

      It works! ftp_pasv($conne ct, true) will turns on passive mode so that
      the ftp client will initiate the data connection and this will work
      around the firewall. Then I enable FTP in the firewall and the script
      can work without turning on the passive mode.

      There is a further problem when I put the above script in a web page as
      shown below.

      <HTML>

      <HEAD>

      <TITLE>

      Getting a directory listing with FTP

      </TITLE>

      </HEAD>



      <BODY>

      <CENTER>

      <H1>Getting a directory listing with FTP</H1>

      Here's what's in the remote directory:

      <BR>

      <BR>

      <?php
      $ftp_server = "myftpserve r";
      $connect = ftp_connect($ft p_server)
      or die("Couldn't connect to $ftp_server.");
      $result = ftp_login($conn ect, "myftpuser" , "mypassword ")
      or die("Couldn't log in $ftp_server.");
      $a = ftp_nlist($conn ect, "/");
      foreach($a as $value){
      echo $value, "\n";
      }
      ?>
      </CENTER>

      <BODY>

      </HTML>

      Then I access it from Firefox with URL
      http://localhost/SpringIntoPHP5/ch09/phpftp.php. The display is:
      ---------------------------------------------------------
      Here's what's in the remote directory:

      Couldn't connect to myftpserver.
      ---------------------------------------------------------

      So it fails at ftp_connect($ft p_server) call. Does the PHP FTP
      function work differently when running from a web page? Is there
      anything related with Firefox? BTW, other examples from this book work
      just fine when viewing from Firefox. Or is it because the script is
      running under the account apache and this account does not have enough
      privilege?

      Thanks again!

      Patrick Schlangen wrote:
      - How to make the FTP functions work?
      >
      You might need to use PASSIVE ftp mode (e.g. because of firewall or NAT
      settings).
      Just try this:
      >
      #! /usr/bin/php
      <?php
      $result = ftp_login($conn ect, "myftpuser" , "mypassword ")
      or die("Couldn't log in $ftp_server.");
      ftp_pasv($conne ct, true);
      $a = ftp_nlist($conn ect, "/");
      ?>
      >
      I just added one line (""ftp_pasv($co nnect, true);"").
      Please let us know if it works now.
      >
      Patrick

      Comment

      • blackpuppy

        #4
        Re: ftp_nlist &quot;425 Can't open data connectoin&quot ;

        At last it's solved. It's because of SELinux settings.

        I am using GNOME. Go to System --Administration --Security Level
        and Filewall. Click SELinux tab. Expand "HTTPD Service", and check
        "Allow HTTPD scripts and modules to connect to the network". Then
        expand "Other", and check "httpd_enable_f tp_server". Finally click OK
        button to close the "Security Level Configuration" dialog.

        After the above changes, the php web page will work.

        Thanks!

        blackpuppy wrote:
        Thanks, Patrick!
        >
        It works! ftp_pasv($conne ct, true) will turns on passive mode so that
        the ftp client will initiate the data connection and this will work
        around the firewall. Then I enable FTP in the firewall and the script
        can work without turning on the passive mode.
        >
        There is a further problem when I put the above script in a web page as
        shown below.
        >
        <HTML>
        >
        <HEAD>
        >
        <TITLE>
        >
        Getting a directory listing with FTP
        >
        </TITLE>
        >
        </HEAD>
        >
        >
        >
        <BODY>
        >
        <CENTER>
        >
        <H1>Getting a directory listing with FTP</H1>
        >
        Here's what's in the remote directory:
        >
        <BR>
        >
        <BR>
        >
        <?php
        $ftp_server = "myftpserve r";
        $connect = ftp_connect($ft p_server)
        or die("Couldn't connect to $ftp_server.");
        $result = ftp_login($conn ect, "myftpuser" , "mypassword ")
        or die("Couldn't log in $ftp_server.");
        $a = ftp_nlist($conn ect, "/");
        foreach($a as $value){
        echo $value, "\n";
        }
        ?>
        </CENTER>
        >
        <BODY>
        >
        </HTML>
        >
        Then I access it from Firefox with URL
        http://localhost/SpringIntoPHP5/ch09/phpftp.php. The display is:
        ---------------------------------------------------------
        Here's what's in the remote directory:
        >
        Couldn't connect to myftpserver.
        ---------------------------------------------------------
        >
        So it fails at ftp_connect($ft p_server) call. Does the PHP FTP
        function work differently when running from a web page? Is there
        anything related with Firefox? BTW, other examples from this book work
        just fine when viewing from Firefox. Or is it because the script is
        running under the account apache and this account does not have enough
        privilege?
        >
        Thanks again!
        >

        Comment

        Working...