PHP Function Reference

PHP ftp_connect() Function



The PHP ftp_connect() function opens an FTP connection to the specified hostname.

Syntax

ftp_connect(hostname, port, timeout)

Parameters

hostname Required. Specify the FTP server address. This parameter should not be prefixed with ftp:// or contain any trailing slashes.
port Optional. Specify an alternate port to connect to. If it is omitted or set to zero, then the default FTP port, 21, will be used.
timeout Optional. Specify the timeout in seconds for all subsequent network operations. Default is 90 seconds. The timeout can be changed and queried at any time with ftp_set_option() and ftp_get_option().

Return Value

Returns a FTP stream on success or false on error.

Example:

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

<?php
//FTP server to use
$ftp_server = "ftp.example.com";
  
//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";
 
  //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!
Connection closed successfully!

❮ PHP FTP Reference