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
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);
Is it possible for a cron job to copy a file from a remote server and move it to our server and over-right the file currently sitting there?
I have looked for an answer on here and not found a suitable solution as most are moving a file to and not from a remote server
there will be two sets of ftp details to include.
This is for a product feed and i really cant get my head around it.
Am i on the right line of thinking with this that i have adapted.
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Yes, it's possible.
Cronjobs are made to execute scripts periodically (every hours, every day, etc...).
So everything you can do with a script, you can do with cronjobs.
after playing around with the code i noticed i was using ftp_put and not ftp_get
here is the working code
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_get($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
I was working on with a FTP upload from my php page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();
$ftp_server = "myserver";
$ftp_user_name="myuser";
$ftp_user_pass="mypass";
$remote_file="myfile.txt";
$file="myfile.txt";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
It gives me a Warning in browser like
Warning: ftp_put(): Access is denied. in /var/www/html/ftpcheck.php on line 17. There was a problem while uploading myfile.txt.
I checked the access permissions on the file. But its accessible. Can any one tell me why this happens?
Most probably this is a permission issue. When you are uploading a file via FTP, you also need to check the directory's permission. When you're saying it's accessible, it doesn't mean it's writable.
You don't check the result of the login operation:
if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
You should also try a manual FTP operation from the PHP host to the FTP host to ensure that you can log-on with these credentials and put a file. This will help you establish whether it's your code at fault or the FTP credentials.
<?php
class Ftp {
function upload()
{
$ftp_server="50.56.113.39";
$ftp_user_name="******";
$ftp_user_pass="***";
$file = "form.pdf";//tobe uploaded
$remote_file = "uploads1/test.pdf";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY))
{
echo "successfully uploaded $file\n";
//exit;
}
else
{
echo "There was a problem while uploading $file\n";
//exit;
}
// close the connection
ftp_close($conn_id);
}
}
?>
Im trying to fetch a csv file from a remote server with ftp_get
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, TRUE);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$okk=0;
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) (line 31)
{
$okk=1;
}
but its giving following error
Warning: ftp_get(): Opening BINARY mode data connection for /abc/abc.csv(198528 bytes). in /home/a/b/c/cm_data/d.php on line 31
I tried changing it to ascii mode then too it gave error
Warning: ftp_get(): Opening ASCII mode data connection for /abc/abc.csv(198528 bytes). in /home/a/b/c/cm_data/d.php on line 31
i also tried using ftp_pasv($conn_id, TRUE); too but still gives error.
What is the problem please help!!
ftp_pasv needs to be called after ftp_login!
You seem not to treat error cases from ftp_connect and ftp_login.
Please try the following code and see if it gives some errors:
<?php
$ftp_server = $ftp_server;
$ftp_user = $ftp_user_name;
$ftp_pass = $ftp_user_pass;
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// try to login
if (#ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected as $ftp_user#$ftp_server\n";
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
$okk=1;
}
} else {
echo "Couldn't connect as $ftp_user\n";
}
// close the connection
ftp_close($conn_id);
?>