PHP Function Reference

PHP ftp_set_option() Function



The PHP ftp_set_option() function sets runtime options of the specified FTP connection.

Syntax

ftp_set_option(ftp, option, value)

Parameters

ftp Required. Specify the FTP connection to use.
option Required. Specify the runtime option to set. Possible options are:
  • FTP_TIMEOUT_SEC - Changes the timeout in seconds used for all network related functions. value must be an integer that is greater than 0. The default timeout is 90 seconds.
  • FTP_AUTOSEEK - When enabled, GET or PUT requests with an offset parameter will first seek to the requested position within the file. This is enabled by default.
  • FTP_USEPASVADDRESS - When set to false, PHP will ignore the IP address returned by the FTP server in response to the PASV command and instead use the IP address that was supplied in the ftp_connect().
value Required. Specify the value of the option parameter.

Return Value

Returns true if the option could be set, false otherwise. A warning message will be thrown if the option is not supported or the passed value does not match the expected value for the given option.

Example:

The example below shows the usage of ftp_set_option() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";
   
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    //setting the timeout value for the ftp connection
    ftp_set_option($ftp, FTP_TIMEOUT_SEC, 120);

    //displaying timeout for the ftp connection
    echo "FTP_TIMEOUT_SEC : "
         .ftp_get_option($ftp, FTP_TIMEOUT_SEC)."\n";
         
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
FTP_TIMEOUT_SEC : 120
Connection closed successfully!

❮ PHP FTP Reference