Warning: ftp_put(): Starting data transfer [duplicate] - php

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);

Related

ftp_put(): Could not create file

I do not know what to do anymore I'm trying to overwrite a file from ftp with windows to a remote linux server using php.
I already tried with ftp_pasv in true, saying that I can not change the mode, I put this line in my file vsftpd.conf: pasv_promiscuous = YES and it does not work either.
Check the route in filezilla and it's the same.
Code php:
$ftp_server = 'ip';
$ftp_user_name = 'user';
$ftp_user_pass = 'pass';
$file = 'C:/archivos/sip_trunk.conf';
$remote_file = '/home/sk/sip_trunk.conf';
$conn_id = ftp_connect($ftp_server) or die("Unable to connect to host");
ftp_pasv($conn_id, true) or die("Unable switch to passive mode");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("Authorization failed");
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
$text = "Upload $file\n";
} else {
$text = "Error Upload $file\n";
}
ftp_close($conn_id);
Error ftp_pasv in false:
ftp_put(): Could not create file.
Arguments:
FTP Buffer resource #315
"/home/sk/sip_trunk.conf"
"C:/archivos/sip_trunk.conf"
1
Error ftp_pasv in true:
Unable switch to passive mode
I do not think it's because I'm running the code on a larvel controller

PHP ftp_put fails

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);

ftp_get works on localhost, but not on production server

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.

Error with connecting ftp through php

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.

ftp php file uploadin

<?php
ini_set('max_execution_time', '0');
$host = '234.546.155.485';
$usr = 'fgfgfgdf';
$pwd = 'fghghh';
// file to move:
$file = 'http://vsomesite.com/file.flv';
$ftp_path = '/public_html/video57242/test.flv';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
// ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
?>
upload fails when the file is remote not local. what problem?
Your variables don't add up. You use $local_file in your ftp_put() call, but you only declare $file earlier in the script
And are you sure you should be using FTP_ASCII for a FLV file? I'd have thought that was binary.
Edit
Scratch my first point. Seems you edited it away :)

Categories