ftp_login() function in PHP

The ftp_login() function in PHP is used to authenticate a user on an established FTP connection. It must be called after ftp_connect() to log in with valid credentials.

Syntax

ftp_login(connection, username, password)

Parameters

  • connection − The FTP connection resource returned by ftp_connect()

  • username − The FTP username for authentication

  • password − The FTP password for authentication

Return Value

The ftp_login() function returns TRUE on successful login or FALSE on failure. A warning is also generated on failure.

Example

The following example demonstrates how to establish an FTP connection and authenticate −

<?php
    $ftp_server = "ftp.example.com";
    $ftp_user = "username";
    $ftp_pass = "password";
    
    // Establish FTP connection
    $connection = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    
    // Attempt to login
    if (ftp_login($connection, $ftp_user, $ftp_pass)) {
        echo "FTP login successful!";
    } else {
        echo "FTP login failed!";
    }
    
    // Close the connection
    ftp_close($connection);
?>

Key Points

  • Always call ftp_connect() before ftp_login()

  • Use @ operator to suppress warnings if needed

  • Close the connection with ftp_close() when finished

  • Anonymous FTP access can use "anonymous" as username with email as password

Conclusion

The ftp_login() function is essential for FTP authentication in PHP. Always verify the login result before performing FTP operations to ensure proper error handling.

Updated on: 2026-03-15T07:40:19+05:30

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements