Sunday, December 21, 2025
HomeLanguagesPHP | ftp_login() function

PHP | ftp_login() function

The ftp_login() function is an inbuilt function in PHP which is used to login to an established FTP connection.

Syntax:

ftp_login( $ftp_connection, $ftp_username, $ftp_userpass );

Parameter: This function accepts three parameters as mentioned above and described below:

  • $ftp_connection: It is required parameter. It specifies the FTP connection to which to login.
  • $ftp_username: It is required parameter. It specifies the username for the FTP Connection.
  • $ftp_userpass: It is required parameter. It specifies the password for the user of that FTP connection.

Return Value: It returns True on success or False on failure.

Note:

  • This function is available for PHP 4.0.0 and newer version.
  • The following examples cannot be run on online IDE. So try to run in some PHP hosting server or localhost with proper ftp server host name, username, password.

Below programs illustrate the ftp_login() function in PHP:
Example 1:




<?php
  
// Connect to FTP server
$ftp_server = "localhost";
  
// Use FTP username
$ftp_username="user";
  
// Use FTP password 
$ftp_userpass="user";
  
// Establish ftp connection 
$ftp_connection = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp_connection) {
    echo "Successfully connected to the ftp server!";
   
    // logging in to established connection with
    // ftp username password
    $login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass);
      
    if($login) {
        echo "<br>Logged in successfully!";
    }
    else {
        echo "<br>Login failed!";
    }
   
    // Closing the connection
    ftp_close($ftp_connection); 
}
  
?>


Output:

Successfully connected to the ftp server!
Logged in successfully!

Example 2: Connect to ftp server using port 21.




<?php
  
// Connect to FTP server
$ftp_server = "localhost";
  
// Use FTP username
$ftp_username="user";
  
// Use FTP password 
$ftp_userpass="user";
  
// Establish ftp connection 
$ftp_connection = ftp_connect($ftp_server, 21) 
    or die("Could not connect to $ftp_server");
   
if($ftp_connection) {
    echo "Successfully connected to the ftp server!";
   
    // logging in to established connection with
    // ftp username password
    $login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass);
      
    if($login) {
        echo "<br>Logged in successfully!";
    }
    else {
        echo "<br>Login failed!";
    }
   
    // Closing the connection
    ftp_close($ftp_connection); 
}
  
?>


Output:

Successfully connected to the ftp server!
Logged in successfully!

Reference: https://www.php.net/manual/en/function.ftp-login.php

RELATED ARTICLES

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS