For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > August 2006 > ftp_nlist "425 Can't open data connectoin"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ftp_nlist "425 Can't open data connectoin"
blackpuppy

2006-08-19, 6:57 pm

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 = "myftpserver";
$connect = ftp_connect($ftp_server)
or die("Couldn't connect to $ftp_server.");
$result = ftp_login($connect, "myftpuser", "mypassword")
or die("Couldn't log in $ftp_server.");
$a = ftp_nlist($connect, "/");
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

2006-08-19, 6:57 pm

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 = "myftpserver";
$connect = ftp_connect($ftp_server)
or die("Couldn't connect to $ftp_server.");
$result = ftp_login($connect, "myftpuser", "mypassword")
or die("Couldn't log in $ftp_server.");
ftp_pasv($connect, true);
$a = ftp_nlist($connect, "/");
foreach($a as $value){
echo $value, "\n";
}
?>

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

Patrick


blackpuppy

2006-08-20, 7:57 am

Thanks, Patrick!

It works! ftp_pasv($connect, 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 = "myftpserver";
$connect = ftp_connect($ftp_server)
or die("Couldn't connect to $ftp_server.");
$result = ftp_login($connect, "myftpuser", "mypassword")
or die("Couldn't log in $ftp_server.");
$a = ftp_nlist($connect, "/");
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($ftp_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:
>
> 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($connect, "myftpuser", "mypassword")
> or die("Couldn't log in $ftp_server.");
> ftp_pasv($connect, true);
> $a = ftp_nlist($connect, "/");
> ?>
>
> I just added one line (""ftp_pasv($connect, true);"").
> Please let us know if it works now.
>
> Patrick


blackpuppy

2006-08-28, 3:57 am

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_ftp_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($connect, 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 = "myftpserver";
> $connect = ftp_connect($ftp_server)
> or die("Couldn't connect to $ftp_server.");
> $result = ftp_login($connect, "myftpuser", "mypassword")
> or die("Couldn't log in $ftp_server.");
> $a = ftp_nlist($connect, "/");
> 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($ftp_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!
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com