On 'localhost' I tried to get a file from FTP server and the local file is successfully created. But when I'm trying in Ubuntu server it's displaying there was a problem and file is not downloading into server. Here is the code. And code file created in this location /var/www/html/
<?php
$local_file = 'whdfcleads.csv';
$server_file = 'hdfc/hdfc_leads.csv';
$ftp_server="*********";
$conn_id = ftp_connect($ftp_server)or die("Couldn't connect to $ftp_server");
$ftp_user_name="*****";
$ftp_user_pass="******";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
else{
echo "There was a problem\n";
}
ftp_close($conn_id);
?>
Please help me to solve this issue, in local host it's working fine but in Ubuntu server local file not creating/downloading.
The error is
Array (
[type] => 2
[message] => ftp_get(): Illegal PORT command
[file] => /var/www/html/wftp.php
[line] => 15
)
The "Illegal PORT command" is a message issued by ProFTPD server, when it receives PORT command with an invalid IP address.
What typically happens, when the client is behind a NAT and reports its internal IP address to the server, not knowing the server is not able to reach back to that IP address.
The easiest (and generally correct) solution is to use an FTP passive mode, instead of an active mode (the default in PHP).
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 my article on FTP connection modes to understand, what the active and passive mode means, and why everyone uses the passive mode nowadays.
Related
I am trying to download from FTP server (using FileZilla server) to my local computer, but I keep getting error
This is my code
<?php
// connect and login to FTP server
$ftp_server = "127.0.0.1";
$ftp_username = "myusername";
$ftp_userpass = "mypassword";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "success connect to FTP";
$local_file = "C:\Users\PROSIA\Desktop\test.docx";
$server_file = "C:\Users\PROSIA\Documents\dummyfile.docx";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
ftp_close($ftp_conn);
This is the error I get:
Warning: ftp_get(C:\Users\PROSIA\Desktop est.docx): failed to open stream:
Error downloading C:\Users\PROSIA\Documents\dummyfile.docx.
My logic for the code is $local_file is the path to save the downloaded file to my local computer and $server_file is the path from FTP server to download the file
So I am a bit confused with the first warning, "failed to open stream" while the file is not exist yet and its seem got blank space (it should be \Desktop\test.docx rather than Desktop est.docx)
And additional question, can I just read, without download?
You cannot use absolute paths to remote files in FTP protocol.
The FileZilla FTP servers has mapping in its configuration that projects the remote file system into virtual FTP file tree. You have to use paths to the virtual file tree.
E.g. The C:\Users\PROSIA can be mapped to something like /users/PROSIA. In that case you use:
$server_file = "/users/PROSIA/dummyfile.docx";
Chances are that you have actually not configured the mapping at all. So you cannot really access the file, until you do the mapping.
Start by connecting to the FTP server with some GUI FTP client, and try to locate the file. The client will show you the correct path to use in your code.
The next problem you have, is that you need to enable the FTP passive mode. The default active mode will hardly work, if you are behind a firewall on NAT. See my article on network configuration needed for active and passive FTP modes.
To switch to the passive mode, use the ftp_pasv function.
ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn, true);
And yes, you can read without downloading.
See
PHP: How do I read a .txt file from FTP server into a variable? or
Stream FTP download to output
My Code is to Upload file to FTP
$conn_id = ftp_ssl_connect($ftp_server, 4480);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, "serverfile.txt", $file, FTP_BINARY);
Its giving this error
PHP Warning: ftp_nb_put(): php_connect_nonb() failed: Operation now in progress (115) in /home/nanobi/PHP/ftp.php on line 51
PHP Warning: ftp_nb_put(): Type set to I in /home/nanobi/PHP/ftp.php
Please help me to solve this
I think you might be experiencing Bug #55651: Option to force PHP to ignore the PASV address returned. There's an issue when an FTP server behind a NAT device returns its local address in response to the PASV command. There's been a patch submitted for this issue years ago, but it hasn't made it into the source yet.
If your FTP server is behind a NAT device, try another FTP client for PHP and see if the issue persists.
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.
I am trying to upload a file to an FTP Server via a PHP script. The connection works and I can create subdirectories on the server. However ftp_put gives the following error:
Warning: ftp_nb_put() [function.ftp-nb-put]: php_connect_nonb() failed: Operation now in progress (115) in ...
Warning: ftp_nb_put() [function.ftp-nb-put]: Type set to I in ....
This is the script
$conn_id = ftp_ssl_connect($ftpHost);#
$login_result = ftp_login($conn_id, $ftpUser, $ftpPw);
ftp_pasv($conn_id, true);
// Verbindung überprüfen
if ((!$conn_id) || (!$login_result))
{
// no Connection
}else{
// Connection WORKS!
if(!#ftp_chdir ( $conn_id , $ftpDir )){
// ftp_mkdir WORKS!
#ftp_mkdir($conn_id, $ftpDir);
}//if(!#ftp_chdir ( $conn_id , $ftpDir )){
if (ftp_put($conn_id, $myFile, $targetfile, FTP_BINARY)) {
// Upload success: NOT WORKING!
}
}
}//if ((!$conn_id) || (!$login_result))
How can I change my script to get my desired behaviour?
As you didn't provide us much useful information, I can only suggest that this is a network issue that prevents opening a data transfer connection.
For details refer to my article on FTP connection modes and necessary network configuration.
If you provide more details, like an FTP server log or any kind of network log (e.g. from Wireshark), you will likely get more concrete answers.
Hoping there is an IIS PHP guru around...
I'm trying to use the standard FTP_PUT command in PHP with passive mode enabled, however I keep getting this error message:
Warning: ftp_put(): php_connect_nonb() failed: No such file or directory (2) in "....root to my script"
Yet, when I try the same script on an Apache server with a different host, it works perfectly! My IIS host says there is nothing their end which would block this, ie. firewall, transfer blocking, etc... Server is running: PHP 5.4.9 - FTP is enabled.
Tried the same script on another IIS server with a different host, PHP verison 5.3.28 - Same problem. Both hosts are baffled.
I've tried different ways to FTP in PHP, using CURL and file_put_contents - Still no joy!
Is there some sneaky configuration required in IIS to make PHP FTP just work!?? Please help...
As requested... Here is my FTP code
$conn_id = ftp_connect($ftpHOST);
// login with username and password
$login_result = ftp_login($conn_id, $ftpUSER, $ftpPASSWORD);
if ((!$conn_id) || (!$login_result)) { //failed connection/login
echo "FTP connection has failed!";
}
else {
// try to upload file
ftp_pasv($conn_id, true); //set passive mode
if (ftp_fput($conn_id, $remoteFILE, $localFILE, FTP_BINARY)) {
echo "Successfully uploaded";
} else {
echo "There was a problem while uploading";
}
}
ftp_close($conn_id); // close the connection