Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I gracefully handle php ftp login error

Tags:

php

ftp

I get a server error on the ftp_login line of the following code. How can I gracefully handle connection/login failure?

$serverAddress = 'ftp.someServerAddress';
$connId = ftp_connect($serverAddress);
if($connId) {
    if (ftp_login($connId, 'UserName', 'password')) {
        // do some stuff
    } else {
        echo 'login failed';
    }
} else {
    echo 'connection failed';
}
like image 369
Rob Avatar asked Oct 30 '22 19:10

Rob


1 Answers

This will work.

if( ! @ftp_login( $connection, 'USERNAME', 'PASSWORD' ) ){
        die( 'Bad login, but no PHP warning thrown.');
}

http://php.net/manual/en/function.ftp-login.php

Sorry forgot to add that the @ symbol before function will suppress errors just as @John Stirling said before me in the comments.

like image 166
Kitson88 Avatar answered Nov 15 '22 02:11

Kitson88