$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if (#ftp_login($ftp_conn, $ftp_username, $ftp_userpass))
{
$path = "./";
$path1="./test";
$file = "test.txt";
$file_list = ftp_nlist($ftp_conn,$path);
}
// close connection
ftp_close($ftp_conn);
The above is the code which I am using. It is working fine for me on my Windows local machine, Windows server machine, Linux local machine, but somehow it fails on the Linux server machine. ftp_nlist returns false. Can someone tell me what might be the reason?
Any help appreciated. Thank you.
Most typical cause of problems with ftp_nlist (or any other transfer command like ftp_get, ftp_put, ftp_rawlist) is that PHP defaults to the FTP active mode. And in 99% cases, one has to switch to the FTP passive mode, to make the transfer working. Use the ftp_pasv function.
$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Passive mode failed");
See also my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command, you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
If none of this help, make sure you have tested, if you can even retrieve the directory listing using any commandline/GUI FTP client running on the same machine as your PHP code. You might not have a programming question in the first place.
Related
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if (#ftp_login($ftp_conn, $ftp_username, $ftp_userpass))
{
$path = "./";
$path1="./test";
$file = "test.txt";
$file_list = ftp_nlist($ftp_conn,$path);
}
// close connection
ftp_close($ftp_conn);
The above is the code which I am using. It is working fine for me on my Windows local machine, Windows server machine, Linux local machine, but somehow it fails on the Linux server machine. ftp_nlist returns false. Can someone tell me what might be the reason?
Any help appreciated. Thank you.
Most typical cause of problems with ftp_nlist (or any other transfer command like ftp_get, ftp_put, ftp_rawlist) is that PHP defaults to the FTP active mode. And in 99% cases, one has to switch to the FTP passive mode, to make the transfer working. Use the ftp_pasv function.
$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Passive mode failed");
See also my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command, you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
If none of this help, make sure you have tested, if you can even retrieve the directory listing using any commandline/GUI FTP client running on the same machine as your PHP code. You might not have a programming question in the first place.
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if (#ftp_login($ftp_conn, $ftp_username, $ftp_userpass))
{
$path = "./";
$path1="./test";
$file = "test.txt";
$file_list = ftp_nlist($ftp_conn,$path);
}
// close connection
ftp_close($ftp_conn);
The above is the code which I am using. It is working fine for me on my Windows local machine, Windows server machine, Linux local machine, but somehow it fails on the Linux server machine. ftp_nlist returns false. Can someone tell me what might be the reason?
Any help appreciated. Thank you.
Most typical cause of problems with ftp_nlist (or any other transfer command like ftp_get, ftp_put, ftp_rawlist) is that PHP defaults to the FTP active mode. And in 99% cases, one has to switch to the FTP passive mode, to make the transfer working. Use the ftp_pasv function.
$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Passive mode failed");
See also my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command, you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
If none of this help, make sure you have tested, if you can even retrieve the directory listing using any commandline/GUI FTP client running on the same machine as your PHP code. You might not have a programming question in the first place.
I have a PC that is running some FTP via PHP that I know used to work 1-2 months ago, but now I return to it I find that the PC is no longer working. I know I have been using the PC but I cannot think of what might have changed.
The PHP is throwing out error messages reading
Unable to build data connection: Connection refused
...when I use the ftp_put() function.
The cut down code I am using is:
<?php
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
$server="***.***.***.***";
$port=21;
echo "<LI>Connecting to $server:$port<BR>";
$conn_id = ftp_connect($server,$port,9999999) or die("<BR>Unable to connect to ".$server.":$port server.");
if ( !$conn_id ) {
$errmsg = $php_errormsg;
echo "<BR><LI>ERR:$errmsg";
}
else {
$passive=false;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
$user="*********";
$pass="*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
$msg = "Failed to login to $selected_server as $user; <BR>check logincredentials in the Settings";
echo "<BR><LI>$msg";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
return $msg;
}
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 10000);
if (!#ftp_put($conn_id, "test.txt", "C:......test.txt", FTP_BINARY)) {
echo "<BR><LI>ftp_put failed";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
}
echo "<HR>Done";
}
?>
the output when running this as a webpage is
Connecting to ***.***.***.***:21
Setting Passive Mode=
Connecting as *******/*****
ftp_put failed
ERR:ftp_put(): Unable to build data connection: Connection refused
Done
The result is that the ftp_put() gives the error message and leaves a zero (0) byte file with the right filename on the server.
The strange thing is is that
the same code/connection info works on another laptop ok
the same connection info works ok using FileZilla when pushing a file
the problem occurs on several servers (ie. it's not just one specific destination that has the problem)
Also, this doesn't seem to have anything to do with the passive mode (it fails with and without this enabled)
Does anyone have any suggestions?
Thanks
Abe
You are using the active FTP mode. In the active mode the server tries to connect to the client. In most network configurations, that's not possible as the client machine is usually behind a firewall.
That's why the server fails with:
Unable to build data connection: Connection refused
It's specifically ProFTPD error message for this situation.
See my article on the active and passive FTP connection modes for details.
The code can work on other machines, if they have firewall disabled or if they have rules that allow incoming traffic on unprivileged ports.
FileZilla works because it defaults to the passive mode (as most modern FTP clients do).
You have claimed to try the passive mode too, yet to get the same error message.
That's because you are using the ftp_pasv call incorrectly.
You have to move the ftp_pasv call after the ftp_login.
$user = "*********";
$pass = "*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
// ...
}
$passive = true;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
The documentation clearly suggests it:
Please note that ftp_pasv() can only be called after a successful login or otherwise it will fail.
For a similar issue (just with Pure-FTPd), see PHP upload via FTP - ftp_put() I won't open a connection to x.x.x.x (only to y.y.y.y).
I have strange problem: I can connect to ftp server, create directory but I can not get a list of files in directory.
What could be wrong?
Facts:
Server is behind firewall
Yes, I use passive mode, as you can see from code
PHP script is on the same server where FTP server is located
I can access FTP from my desktop using FTP client, and everything works normal, I can create/upload and read contents of directories.
ftp_pwd() and ftp_mkdir() work fast, ftp_nlist() and ftp_rawlist() wait for time-out specified in ftp_connect() (10 seconds in example case)
In active mode - almost the same results (red-dir-functions fail immediately)
I think that problem not in the script but in ftp/server/firewall/access rights setup.
There is my code example:
$conn = ftp_connect('my.host.here', 21, 10);
if (!$conn) {
throw new Exception('Unable to connect to FTP');
}
$login_result = ftp_login($conn, 'login', 'password');
if (!$login_result) {
throw new Exception('Unable to login to FTP');
}
// This is important part, because my server is behind frirewall/nat.
$paswRes = ftp_pasv($conn, true);
if (!$paswRes) {
throw new Exception('Failed to enable passive FTP mode');
}
$resPwd = ftp_pwd($conn); //Returns: "/"
$resMkdir = ftp_mkdir($conn , 'testDir'); //Returns: "/testDir", creates new dir
$resNlist = ftp_nlist($conn, '.'); //Returns: bool(false)
$resRawlist = ftp_rawlist($conn, '.'); //Returns: bool(false)
Upd:
It works if host is localhost. So looks like problem is in firewall/ftp server setup.
I use ProFtpd on Amazon EC2
Passive ports specified as PassivePorts 49152 65534
This range has been added as inbound port range in Amazon EC2 Security Groups 49152 - 65534 0.0.0.0/0
Ok, I have solved the problem.
It was FTP server misconfiguration (in my case - ProFTPD server):
Besides PassivePorts I had to specify MasqueradeAddress
MasqueradeAddress <your-Ftp-Server-Domain-Name-Or-Ip-Address>
Upd: Also, check this article: http://www.elitehosts.com/blog/php-ftp-passive-ftp-server-behind-nat-nightmare/
I'm trying to list some files from an external FTP server using php ftp functions on a Windows shared hosting, but I'm having several problems.
I firstly tried with a couple of web applications like ajaxplorer and net2ftp, but I got frustrated and I decided to make a very basic script for testing..
<?php
$ftp_server = "alinuxftpserver";
$ftp_user = "user";
$ftp_pass = "pass";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// change temp folder (windows)
putenv("TMP=D://inetpub//webs//domain//net2ftp//tmp");
echo getenv('TMP');
// try to login
if (#ftp_login($conn_id, $ftp_user, $ftp_pass))
{
echo "Connected as $ftp_user#$ftp_server\n";
}
else
{
echo "Couldn't connect as $ftp_user#$ftp_server\n";
}
if(ftp_pasv( $conn_id, true ))
echo "Passive mode, it worked<br/>";
else
echo "Passive mode, it didn't work<br/>";
$contents = ftp_rawlist($conn_id, ".");
var_dump($contents);
ftp_close($conn_id);
die;
?>
On my localhost (linux) it returns an array, while on the windows hosting it returns:
Warning: ftp_rawlist() [function.ftp-rawlist]: php_connect_nonb() failed: No such file or directory (2) in D:\inetpub\webs\domain\ftp.php on line 26
bool(false)
Can't understand.. the directory should be "/" on the external ftp server and of course there are some files & folders (2 folders and 1 file).. In fact on my MAMP installation it works well.
Hosting guys told me that the server configuration is ok.
use ftp_pasv($conn_id, true); some ftp connections will work in passive mode only
I'm not 100% sure, but I guess, you should use $contents = ftp_rawlist($conn_id, "/");
instead of $contents = ftp_rawlist($conn_id, ".");
Check your FTP server logs. In my case, the pasv_address was set to a wrong IP address.
Better late than never... I had the same problem. With a Linux server everything worked great, but with Windows Server (many versions) we had many problems, including with ftp_nlist() returning an empty array. This worked for us, but I don't know why!
ftp_nlist($handler, '*');