I have migrated my code from xampp to lamp recently. Since that time I have a problem with ftp_connect function and it always returns false. Here is the code:
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
Is there any setting in PHP or apache I have to set in advanced!?
This example works for me:
$ftp_server = "SERVER IP";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "YOUR USERNAME";
$ftp_user_pass = "YOUR PASSWORD";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$contents = ftp_nlist($conn_id, '/');
for ($i = 0 ; $i < count($contents) ; $i++)
echo "<li>" . substr($contents[$i],1) . "</li>";
ftp_close($conn_id);
Try running this:
<?php
$c = ftp_connect('ftp.mozilla.org');
var_dump($c);
$c = ftp_connect('abcdefg');
var_dump($c);
?>
You should get this:
resource(2) of type (FTP Buffer) Warning: ftp_connect()
[function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\abc\def.php on line 5
bool(false)
Answer on : How to get error if FTP server is invalid.?. Then you will know what kind of error appears.
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 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
I'm trying to download file via php script, but unfortunately with this script i get error:
PHP Warning: ftp_connect(): php_network_getaddresses: getaddrinfo
failed: nodename nor servname provided, or not known in
/Users/apple/projects/asystem/download_dump.php on line 15
i try to put different FTP address test it and it worked fine
so the problem is 100% in the server address...
// define some variables
$folder_path = "/Users/apple/projects/asystem";
$local_file = "auct_lots_full.xml.zip";
$server_file = "auct_lots_full.xml.zip";
//-- Connection Settings
$ftp_server = "ftp://xxxx_user:Eecohshxxxxxx#auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
#$destination_file = "FILEPATH";
// 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);
// try to download $server_file and save to $local_file
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";
}
// close the connection
ftp_close($conn_id);
?>
so yes! the problem was that i use wrong path to the FTP. This code works:
//-- Connection Settings
$ftp_server = "auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
I'm currently making a script that will backup files that I have on my server but am having a major issue. About 30 minutes ago I was able to connect using the same script, list my files, and download them. But now when I try to connect, it connects and that is it, I can't ftp_nlist the files because when I do it just returns bool(false). I read on some other posts that I should try setting passive mode to true with ftp_pasv but it did not change anything. I've also tried other users which do the same exact thing. I tried using the credentials on filezilla and it connected and allowed me to download with no issues. Any help would be greatly appreciated, I'll post the code below.
PHP:
Connect to FTP
$ftp_server = "Hidden for security";
$ftp_user = "Hidden for security";
$ftp_pass = "Hidden for security";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_pasv($conn_id, true);
Get filenames on FTP
$contents = ftp_nlist($conn_id, ".");
$rm = array(0, 1, 2, 3,);
foreach($rm as $value) {
unset($contents[$value]);
}
$contents = array_values($contents);
Get filenames on localhost
$dir = scandir("images/");
$rmval = array(0, 1, 2);
foreach ($rmval as $val) {
unset($dir[$val]);
}
$dir = array_values($dir);
Try to login
if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
//echo "Connected";
$counter = 0;
var_dump($contents);
/*foreach ($ftp_content as $file) {
ftp_get($conn_id, "images/" . $ftp_content[$counter], $ftp_content[$counter], FTP_BINARY);
$counter++;
}*/
} else {
echo "Couldn't Connect";
}
Close FTP connection
ftp_close($conn_id);
The issue was that I was trying to list my files before establishing a connection to the FTP server. Adding $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass) after $conn_id fixed my issue. Now I'm able to evaluate if it's true or false and run different code depending on the result.
I'm trying to get a directory listing from a FTP server which only accepts FTP connections over explicit TLS.
<?php
$ftp_server = "...";
$ftp_user_name = "...";
$ftp_user_pass = "...";
// set up basic ssl connection
$conn_id = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
echo $login_result . "<br/>";
// change dir to "data"
$chdir_result = ftp_chdir($conn_id, "data");
echo $chdir_result . "<br/>";
echo ftp_pwd($conn_id) . "<br/>";
// get contents of the current directory
$contents = ftp_rawlist($conn_id, ".");
// output $contents
var_dump($contents);
// close the ssl connection
ftp_close($conn_id);
?>
The script hangs for about a minute and gives the following output:
1
1
/data
bool(false)
I also tried using ftp_nlist and print_r with no avail. (print_r just prints blank).
If you have a firewall or NAT enabled on your server then you have to use passive mode for your ftp connection
Add this before ftp_rawlist()
ftp_pasv( $conn_id, true );