I'm trying to connect to my server using php script to upload some files...
But it doesn't connect...
I dont know what is the error...
I'm sure that ftp is enable, i checked it through php_info()
What may be the error...
<?php
error_reporting(E_ALL);
$ftp_server = "server.com"; //address of ftp server (leave out ftp://)
$ftp_user_name = "Username"; // Username
$ftp_user_pass = "Password"; // Password
$conn_id = ftp_connect($ftp_server); // set up basic connection
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
echo "Connected as ,$ftp_user_name,$ftp_user_pass \n";
} else {
echo "Couldn't connect \n";
}
.....
.....
....
....
ftp_close($conn_id); // close the FTP stream
?>
maybe you have to turn on the passive mode by doing:
ftp_pasv($conn_id, true);
directly after your ftp_login
PS: why do you do a double login? write
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result) {
instead of
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
This looks wrong to me:
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
You should just need:
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result) {
Otherwise it will attempt to log in twice, this could be the issue.
Also try adding or die to the ftp_conect to see if it can even connect to server.
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
Check the server you're trying to connect actually accepts connections from wherever this script is running from by using a regular FTP client. Your code may be correct but the FTP server isn't accepting connections from your server, or there's a firewall blocking things.
Most PHP functions will log error information internally that you can retrieve with error_get_last() and/or $php_errormsg. Some diagnostic information as to why the login call is failing may be stored in there.
Related
I upload XML file through FTP:
$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}
I want to send the XML file created using DOMDocument to a FTP server but I am not able.
The ftp_put returns false.
Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the 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("Unable switch to passive mode");
The ftp_pasv must be called after ftp_login. Calling it before has no effect.
See also:
PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
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 (what is quite common, if the server is behind firewall/NAT), 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.
This worked:
// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";
// upload file
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
echo "Successfully uploaded $file.";
} else {
echo "Error uploading $file";
}
// close connection
ftp_close($ftp_conn);
I upload XML file through FTP:
$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}
I want to send the XML file created using DOMDocument to a FTP server but I am not able.
The ftp_put returns false.
Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the 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("Unable switch to passive mode");
The ftp_pasv must be called after ftp_login. Calling it before has no effect.
See also:
PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
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 (what is quite common, if the server is behind firewall/NAT), 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.
This worked:
// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";
// upload file
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
echo "Successfully uploaded $file.";
} else {
echo "Error uploading $file";
}
// close connection
ftp_close($ftp_conn);
I've tried just about everything, but unable to form a connection using ftp_connect. There is no error message, so I don't know where to look. It just times-out.
Here is the code:
<?php
$remote_file = 'file.txt';
$ftp_host = '1.1.1.1';
$ftp_user_name = 'root#1.1.1.1';
$ftp_user_pass = 'password';
$local_file = 'file.txt';
$connect_it = ftp_connect($ftp_host) or die("Could not connect");
$login_result = ftp_login($connect_it, $ftp_user_name, $ftp_user_pass);
if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) {
echo "WOOT! Successfully transfer $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
ftp_close( $connect_it );
?>
Both servers are mine so I know they are both on and working. The server is trying to connect and unless I set up a time out it just keeps going and going but the connection is not made. All I'm trying to do is transfer one text file from one VPS to another.
If your connection simply times out without error, this means there is a port that isn't opened on one server or the other.
Here is what you could try:
maybe turn error reporting on:
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
check on both servers that the port you intend to use is opened
$ netstat -a|grep ftp
tcp 0 0 *:ftp *:* LISTEN
(here you try to use the default that should be "21")
check that you're able to telnet the thing
telnet <host> <port>
Then try just the minimal ftp connect
$ftp_host='1.1.1.1';
$ftp_user_name = 'root'; // I think in your case you should not add '#1.1.1.1'...
$ftp_user_pass = 'password';
$conn_id = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");
// Then chdir to a directory:
ftp_chdir($conn_id, "/your/dir");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
See the full documentation of PHP's FTP functions.
I am using ftp_get to fetch a file from another ftp server.
My code works perfectly on my local WAMP server, but when I apply it to my actual web hosting, the connection fails.
$conn = ftp_connect('ftp.server.com') or die('Could not connect');
ftp_login($conn,'myusername','mypassword');
$local_file = 'new/two.txt';
$remote_file = 'games/minecraft/craftbukkit/plugins/VisitCounter/config.yml';
$get = ftp_get($conn,$local_file,$remote_file,FTP_ASCII);
if ($get){
echo 'Connection Successful';
} else{
echo 'Connection failure';
}
exec($get);
ftp_close($conn);
I'm thinking that something required to establish a ftp connection must be disabled on my web server.
Can any of you assist?
Thanks,
Jared
Your server is not in a passive mode,You just need to turns on passive mode of your server using the php function as :-
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
$ps = ftp_pasv($conn_id, true);
Hope it will work.
Variation of my question on Ubuntu.SE:
This is (basically) what I'm doing when I log into a FTP:
ftp user:password#server
ftp: user:password#server: Unknown host
ftp> echo HELLO WORLD!
ftp> quit
Is it possible to "echo" over ftp in PHP?
<?php
$ftp_server = "server";
$ftp_user_name = "user";
$ftp_user_pass = "SuperSecretPassword";
$message = "Hello World!";
// connect
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Echo Message
$upload = ftp_echo($conn_id, $message);
// close the FTP stream
ftp_close($conn_id);
?>
Maybe I'm an idiot, but all the commands I see are for pushing, pulling or doing stuff locally. Does something else act as 'ftp> echo "Hello World!"' and am I'm looking right at it without realizing it?
I think you want ftp_raw. You'd use this to put an arbitrary command to your ftp server.
<?php
$fp = ftp_connect("ftp.example.com");
/* This is the same as:
ftp_login($fp, "joeblow", "secret"); */
ftp_raw($fp, "USER joeblow");
ftp_raw($fp, "PASS secret");
?>