Syntax:ftp_connect ($ftp_host, $ftp_port, $timeout);
Parameter:this function takes three parameters as above and described below:
- $ftp_host:This is a required parameter and is used to specify the hostname or ftp server to connect to. It can be a domain name or an IP address, and these addresses must not be prefixed with "ftp: //" or must not have a slash at the end of this URL.
- $ftp_port: this is an optional parameter. It specifies the port number to connect. If not specified, the default FTP port number is used. The default FTP port number is - 21.
- $timeout:This is an optional parameter. It specifies the waiting time for all subsequent network operations. If this parameter is not specified, the default is 90 seconds.
Note.The timeout can be requested or changed at any time using ftp_get_option() and ftp_set_option() respectively.Return Value:Returns FTP stream on success or False on error.Notes: - This function is available for PHP 4.0.0 and newer.
- The following examples cannot be run in the online IDE. So try running PHP on some hosting server or localhost with the correct ftp server name.
The following programs illustrate the ftp_connect() function in PHP:Example 1:
// Connect to the FTP server
$ftp_server
=
"localhost"
;
// Establish FTP connection
$ftp_connection
= ftp_connect (
$ftp_server
)
or
die
(
"Could not connect to $ftp_server"
);
if
(
$ftp_connection
) {
echo
"Successfully connected to the ftp server!"
;
// Close the connection
ftp_close (
$ftp_connection
);
}
?>
Output:Successfully connected to the ftp server!
Example 2.Connecting to the ftp server on port 21.
// Connect to the FTP server
$ftp_server
=
"localhost"
;
// Establish FTP connection
$ftp_connection
= ftp_connect (
$ftp_server
, 21)
or
die
(
"Could not connect to $ftp_server"
);
// port 21 is used as the second parameter
// in the ftp_connect() function
if
(
$ftp_connection
) {
echo
"Successfully connected to the ftp server!"
;
// Close the connection
ftp_close (
$ftp_connection
);
}
?>
Output:Successfully connected to the ftp server!
Link: https://www.php.net/manual/en/function.ftp-connect.php