I am trying to connect to multilple ftp servers using ftp_connect
and ftp_login
. My code is very simple as given
foreach ($ftp_accounts as $ftp_account) {
$connect_id = $ftp_account['server'];
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user
";
}
}
I have a table in my database with multiple servers and their credentials. but the code i wrote give me the following error (warning) message
ftp_login() expects parameter 1 to be resource, string given
and it goes to the else statement for every server.
Any idea what I am doing wrong ?
Thanks in advance
Let me answer my question. First of all the replies I get was all corect options to be checked for such problems I highly appriciate the replies but one thing you also need to check is that wether your server firelwall allows to connect to any outgoing ftp server or not ? If it does not allow you to connect then even with everything written correct in your code you will not be able to connect to the server. So before testing your code check your server firewall settings.
It worked for me when i allowed all outgoing connections.
Once again Thanks for the replies
Replace
<?php
$connect_id = $ftp_account['server'];
?>
with
<?php
$connect_id = ftp_connect($ftp_account['server']);
?>
Hope this helps.
You forgot to make connection to server using ftp_connect
function, assuming that in variable $ftp_account['server']
you have valid FTP server host address.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if ($connect_id === false) {
echo "Failed to connect to FTP server";
continue;
}
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: ' . $ftp_account['server'] . '<br> Username: ' . $user . '<br> Password: ' . $pass . '<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $ftp_account['server'] . " as " . $user . '<br>';
} else {
echo "Couldn't connect to " . $ftp_account['server'] . " as " . $user . "
";
}
}
You need to use ftp_connect()
to connect to the server before trying to login. Additionally, you need to check the result of ftp_connect()
to ensure it was able to connect before you try to login. If it wasn't then it returns FALSE
and you should not proceed to try and login.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if($connect_id !== false)
{
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user
";
}
} else {
echo 'failed to connect to server';
}
}