PHP ftp_login() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_login() logs in to an FTP server using a username and password. It is the step you run right after ftp_connect() and before any file or directory command.

Opening a connection only reaches the server. The server will refuse most commands until you prove who you are, so ftp_login() sends your username and password and tells you if they were accepted.

Syntax

Call ftp_login() with the values you need. Start with this line.

PHP
$loggedIn = ftp_login($ftp, 'username', 'password');
CallWhat it does
ftp_login($ftp, 'username', 'password')Returns true when the server accepts the login, false when it does not.
ftp_login($ftp, 'anonymous', '[email protected]')The usual way to log in to a public anonymous FTP server.
@ftp_login($ftp, $user, $pass)Hides the warning PHP raises on failure so you can handle false yourself.

A failed login raises an E_WARNING and returns false. Always check the result before running other FTP functions. Plain FTP sends the password in clear text, so prefer ftp_ssl_connect() for real accounts.

Library declarations
PHP
ftp_login(FTP\Connection $ftp, string $username, string $password): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
usernameRequired. The FTP username. Use "anonymous" for public servers.
passwordRequired. The password for that user. For anonymous logins, an email address is customary.

Return Value

Return ValueDescription
booltrue on success, or false if the login fails.

PHP Examples

Example 1 — Connect and Log In

Open a connection, log in with placeholder credentials and report the result.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');

if (ftp_login($ftp, 'username', 'password')) {
    echo "Logged in successfully", PHP_EOL;
} else {
    echo "Login failed", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_connect() reaches the server but does not log you in yet.
  2. ftp_login() sends the username and password to the server.
  3. The if statement checks the returned boolean.
  4. ftp_close() ends the connection at the end of the script.

Example 2 — Handle a Wrong Password

A wrong password makes ftp_login() return false and raise a warning. Use @ and check the result.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');

$ok = @ftp_login($ftp, 'username', 'wrong-password');

if (!$ok) {
    echo "Could not log in. Check your username and password.", PHP_EOL;
    ftp_close($ftp);
    exit(1);
}

echo "Welcome!", PHP_EOL;

How It Works

  1. The password does not match, so the server rejects the login.
  2. The @ operator hides the PHP warning so the page stays clean.
  3. Because $ok is false, the script prints a clear message.
  4. The connection is closed and the script stops with exit(1).

Example 3 — Log In Over FTPS With Passive Mode

Use a secure connection, log in with values from environment variables, then enable passive mode.

PHP
<?php
$host = getenv('FTP_HOST') ?: 'ftp.example.com';
$user = getenv('FTP_USER') ?: 'username';
$pass = getenv('FTP_PASS') ?: 'password';

$ftp = ftp_ssl_connect($host);

if ($ftp && ftp_login($ftp, $user, $pass)) {
    ftp_pasv($ftp, true);
    echo "Secure login OK, passive mode on", PHP_EOL;
    ftp_close($ftp);
}

How It Works

  1. getenv() reads the connection details so no secrets live in the file.
  2. ftp_ssl_connect() opens an encrypted connection.
  3. ftp_login() runs only if the connection succeeded, thanks to the && check.
  4. ftp_pasv() is called after login, which is the correct order.

Frequently Asked Questions

It logs in to an FTP server with a username and password. You must call it after ftp_connect() and before most other FTP functions.
It returns false and raises an E_WARNING. You can silence the warning with @ and check the returned boolean yourself.
Use the username anonymous and your email address as the password, like ftp_login($ftp, 'anonymous', '[email protected]').
On a normal FTP connection the password is sent as plain text. Use ftp_ssl_connect() to create an encrypted connection first, or use SFTP with another library.
No. ftp_pasv() only works after a successful login, so always call ftp_login() first.

Next: ftp_mdtm()

ftp_mdtm() returns the last modified time of a file on an FTP server. The result is a Unix.

ftp_mdtm() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful