PHP ftp_get_option() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_get_option() reads one of the runtime settings of an open FTP connection. It tells you the current timeout, whether auto-seek is on, and whether the server's passive address is used.

Think of it as the "read" partner of ftp_set_option(). You pass the connection and one option constant, and PHP gives you the current value back, so you can check a setting before you change it.

Syntax

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

PHP
$timeout = ftp_get_option($ftp, FTP_TIMEOUT_SEC);
CallWhat it does
ftp_get_option($ftp, FTP_TIMEOUT_SEC)Returns the network timeout in seconds as an int (the default is 90).
ftp_get_option($ftp, FTP_AUTOSEEK)Returns true or false depending on whether auto-seek is enabled.
ftp_get_option($ftp, FTP_USEPASVADDRESS)Returns true if PHP uses the IP address the server sends in passive mode.

Only three options are supported: FTP_TIMEOUT_SEC, FTP_AUTOSEEK and FTP_USEPASVADDRESS. In PHP 8, any other value throws a ValueError. The first argument is an FTP\Connection object, not a resource.

Library declarations
PHP
ftp_get_option(FTP\Connection $ftp, int $option): int|bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
optionRequired. One of FTP_TIMEOUT_SEC, FTP_AUTOSEEK or FTP_USEPASVADDRESS.

Return Value

Return ValueDescription
intThe timeout in seconds when you ask for FTP_TIMEOUT_SEC.
booltrue or false when you ask for FTP_AUTOSEEK or FTP_USEPASVADDRESS.

PHP Examples

Example 1 — Read the Default Timeout

Connect to a server and print the timeout that PHP uses for network operations.

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

$timeout = ftp_get_option($ftp, FTP_TIMEOUT_SEC);
echo "Timeout: ", $timeout, " seconds", PHP_EOL;

ftp_close($ftp);

How It Works

  1. ftp_connect() opens a connection to the placeholder host and gives you an FTP\Connection object.
  2. ftp_get_option() with FTP_TIMEOUT_SEC asks for the current timeout.
  3. PHP returns 90 because that is the default when you do not pass a timeout to ftp_connect().
  4. ftp_close() ends the connection when you are done.

Example 2 — Catch an Unsupported Option

In PHP 8, asking for an option that does not exist throws a ValueError. Catch it so your script keeps running.

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

try {
    $value = ftp_get_option($ftp, 12345);
    var_dump($value);
} catch (ValueError $e) {
    echo "Unsupported option", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The number 12345 is not one of the three supported option constants.
  2. PHP 8 throws a ValueError instead of returning false.
  3. The catch block handles the error and prints a friendly message.
  4. The connection is still valid, so ftp_close() runs normally.

Example 3 — Change a Setting and Read It Back

Use ftp_set_option() to change the timeout, then confirm the new value with ftp_get_option().

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

echo "Before: ", ftp_get_option($ftp, FTP_TIMEOUT_SEC), PHP_EOL;

ftp_set_option($ftp, FTP_TIMEOUT_SEC, 30);

echo "After: ", ftp_get_option($ftp, FTP_TIMEOUT_SEC), PHP_EOL;
echo "Auto-seek: ", var_export(ftp_get_option($ftp, FTP_AUTOSEEK), true), PHP_EOL;

ftp_close($ftp);

How It Works

  1. The first call reads the default timeout of 90 seconds.
  2. ftp_set_option() changes the timeout to 30 seconds for this connection.
  3. The second ftp_get_option() call shows the new value.
  4. var_export() prints the boolean result of FTP_AUTOSEEK as true.

Frequently Asked Questions

It returns the current value of a runtime option on an FTP connection. You can read the timeout, the auto-seek flag and the passive-address flag.
Only FTP_TIMEOUT_SEC, FTP_AUTOSEEK and FTP_USEPASVADDRESS. Any other value throws a ValueError in PHP 8.
The default is 90 seconds. You can pass a different value as the third argument of ftp_connect() or change it with ftp_set_option().
ftp_get_option() only reads a setting, while ftp_set_option() changes it. They accept the same option constants.
No. These settings belong to the connection object, so you can read them right after ftp_connect(), even before ftp_login().

Next: ftp_login()

ftp_login() logs in to an FTP server using a username and password. It is the step you run.

ftp_login() 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