I have a code I am trying to connect to a different server via PHP FTP connection.
I know that I am actually connecting to the server.
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
var_dump($login);
when I vardump the $login, I get a TRUE.
When I try to upload a file I get a 'error uploading file'
so I tried to just pull a list of files on the connection:
$file_list = ftp_nlist($ftp_conn, ".");
var_dump($file_list);
It's only returning bool(false).
I know the connections has files because I can view them via FileZilla with the same credentials.
Any idea what might be wrong? Is it possible a server setting that isn't allowing me to use this PHP script from a shared server?
Most typical cause of problems with ftp_list (ftp_nlist, ftp_put and other functions that require separate FTP data connection) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the directory listing and transfer working. Use the ftp_pasv function.
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass) or die("Login failed");
// turn passive mode on
ftp_pasv($ftp_conn, true) or die("Unable switch to passive mode");
See my article on the active and passive FTP connection modes.
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.
$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 can not overwrite files on remote server while uploading using php script ** ftp_put();
Is there any option or function to enable overwrite in ftp_put
Try this.!!
If ft_put fails try ftp_pasv (Passive mode).
$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);
I cannot upload files into my ftp server.
There is always warning:
ftp_put(): Connecting to port.
<?php
set_time_limit(0);
$host = 'xxxx';
$usr = 'yyyy';
$pwd = 'zzzz';
$local_file = '/home/back.sql';
$ftp_path = '/public_html/';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path.'back1.sql', $local_file, FTP_ASCII);
print($upload);
?>
The code were executed for three times. I got three different warnings.
Warning: ftp_put(): Connecting to port 1926 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1928 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1930 in filename (i named it) on line 10
What does the warning info mean?
Why connect to different ports? Maybe the ports should be 21 for every time, why not?
The "Connecting to port xxx" is a message issued by PureFTPD server, when it tries to connect back to the FTP client to its active mode data connection port (which is random, that's why it changes).
If you really need to use the active mode, you need to allow incoming connections to the active mode data connection port range used by PHP.
See my guide for network configuration necessary for the active mode FTP.
Though, if you do not need to use the active mode, use the passive mode instead. The passive mode generally does not require any network configuration on a client side.
In PHP, you switch to the passive mode by calling the ftp_pasv function after the ftp_login:
...
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
...
See the above guide to understand the difference between the active and the passive FTP mode.