In this PHP tutorial, we will show you how to connect to an FTP server and then login into it.
To do this, we will use the functions ftp_connect and ftp_login.
Take a look at our example login script below:
//The IP or hostname of the FTP server. $ftpHost = '127.0.0.01'; //The FTP username. $ftpUsername = 'test'; //The FTP password. $ftpPassword = 'password'; //Attempt to connect to the FTP server by using //PHP's ftp_connect server. $ftpConnection = @ftp_connect($ftpHost); //If ftp_connect returns a boolean FALSE value, then //we failed to connect if($ftpConnection === false){ //Throw an exception throw new Exception('Could not connect to FTP server at ' . $ftpHost); } //If our FTP connection is successful, we can now attempt //to login to the server. //We can do this by using the ftp_login function. $loggedIn = @ftp_login($ftpConnection, $ftpUsername, $ftpPassword); //If ftp_login returns a boolean FALSE value, then we //failed to login to the FTP server. if($loggedIn === false){ $phpErrMessage = error_get_last(); if(!empty($phpErrMessage)){ $phpErrMessage = $phpErrMessage['message']; } throw new Exception('Could not login to FTP server: ' . $phpErrMessage); }
In the code snippet above, there are three important variables that you will need to change:
- $ftpHost: This is the IP address or host name of the FTP server that you want to connect to.
- $ftpUsername: You will need to change this to match your FTP username.
- $ftpPassword: You will need to change this to match your FTP password.
1: Connect to the server with ftp_connect.
Our script connects to the FTP server using PHP’s ftp_connect function.
If the ftp_connect function returns a boolean FALSE value, then it means that we were unable to connect to the server.
This can be for a number of reasons:
- The IP address or host name is incorrect.
- The FTP server is not running.
- Your IP address has been blacklisted by the server.
- A firewall issue is preventing you from connecting.
In our code, we throw an Exception if the FTP connection cannot be established.
As a result, our PHP script will terminate if the connection fails. This makes sense, as there is no point in attempting to login if we are unable to connect.
If a valid connection can be made, then the ftp_connect function will return an FTP stream resource.
Step 2: Log into the FTP server using ftp_login.
Once the connection is successful, we can attempt to log into the server using PHP’s ftp_login function.
This function takes three parameters:
- The FTP stream that ftp_connect returned.
- Our FTP username.
- Our FTP password.
If the login attempt fails, then the ftp_login function will return a boolean FALSE value.
By default, the function will also omit a warning message such as:
Warning: ftp_login(): Permission denied.
In the ftp_login code above, we are intentionally suppressing this warning message with the @ symbol.
Instead, we throw an Exception with the last known error.
And that’s it. If your FTP server details are filled out correctly, then the above script should execute without any exceptions being thrown.
See also: Navigate to an FTP directory using PHP.