PHP ftp_nlist() - timeout - php

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/

Related

ftp_rawlist returns false when connected [duplicate]

$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.

Setting up an ftp_get [duplicate]

$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.

Unable to run FTP commands from AWS AMI

I want to connect to an FTP using PHP to upload the reports generated. As per the remote server, the FTP needs to be in ACTIVE mode.
this is my code:
ini_set('display_errors', '1');
error_reporting(E_ALL);
$conn_id = ftp_connect('myftpserver.com', 21);
if($conn_id)
{
// login with username and password
$login_result = ftp_login($conn_id, 'mysuer', 'password');
$passive = ftp_pasv($conn_id,FALSE);
echo "is active?<br/>";
var_dump($passive);
echo 'Login Result:';
var_dump($login_result);
$files_list = ftp_nlist($conn_id, '/MyFolder/');
echo "<br/>files list ";
var_dump($files_list);
}
else
{
var_dump('Unable to connect to FTP Server');
}
When I am running it from the local machine or a normal shared server, I am able fetch the list, but I am unable to run the script from my AWS AMI instance. For testing purpose, I have even opened all inbound traffic too. Still no luck. Also, the point is that if I try with some other ftp details, I am able to get the response of ftp_nlist. But not for this one. I tried it on 3 AWS instances till yet. Yielded the same result.
All I can say is that this is somewhere the issue at my server security group/firewall. But unable to figure it out. Please help.
The response I get from the server:
is active
bool(true)
Login Result:bool(true)
files list bool(false)
Using FTP Active Mode is problematic with AWS Security Groups.
For active mode to work, you will have to open all inbound ports above 1023. If your client supports restricting the range, do so. You will also need to open both port 20 and port 21 inbound and outbound.
The problem is that the FTP client selects a port that it will listen on. Then the FTP client informs the FTP Server of this port number. The FTP Server then connects to this port. This goes against normal AWS Security Group designs meaning only allow specific ports to be open. You can verify this by opening all ports temporarily, test your FTP client and then closing all the ports.
Active Mode is not secure for the FTP client. Passive Mode is not secure for the FTP server (but the better choice).
NOTE: Rotate your FTP credentials often. Your login and password is sent in the clear and are not encrypted.
FTP is a legacy technology, which is still very popular, that should be stored away in the attic.

PHP ftp_nlist is not working

$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.

php script on Windows hosting does not list ftp files (ftp_rawlist issues)

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, '*');

Categories