PHP | ftp_ssl_connect() Function
Last Updated :
07 Aug, 2021
The ftp_ssl_connect() function is an inbuilt function in PHP which opens a secure SSL-FTP connection. FTP functions can be run against the server while the connection is open. It opens an explicit SSL-FTP connection to the host specified in parameter. It will succeed even if the server’s certificate is invalid or is not configured for SSL-FTP.
Syntax:
ftp_ssl_connect($host, $port, $timeout);
Parameters: This function accepts three parameters as mentioned above and described below:
- $host: It is required parameter. It specifies the FTP server address. It can be a domain address or an IP address. This parameter shouldn’t be prefixed with “ftp://” or shouldn’t have any trailing slashes.
- $port: It is an optional parameter. It specifies the port to connect to. If no port is provided then default port is used i.e. 21.
- $timeout: It is also an optional parameter. It specifies the timeout for network operations. If not provided then default value is passed which is 90 seconds.
Return Value: It returns SSL-FTP stream on success or false on failure.
Note:
- This function is available on PHP 4.0.0 and newer version.
- The following examples cannot be run on online IDE. So try to run in some PHP hosting server or localhost with proper ftp server name.
- This function is only available if both the ftp module and the OpenSSL support is built statically into PHP.
Below programs illustrate the ftp_ssl_connect() function in PHP:
Example 1:
PHP
<?php
$conn = ftp_ssl_connect( "ftp.testftp.com" )
or die ( "Could not connect" );
echo "Connection established successfully" ;
?>
|
Output:
Connection established successfully
Example 2: This example uses ftp_ssl_connect() function to logging in using SSL-FTP Connection
PHP
<?php
$ftp_server = "8.8.8.8" ;
$ftp_conn = ftp_ssl_connect( $ftp_server )
or die ( "Could not connect to $ftp_server" );
$ftp_username = "your_username" ;
$ftp_userpass = "your_password" ;
$login = ftp_login( $ftp_conn , $ftp_username , $ftp_userpass );
if ( $login ) {
echo "Successfully logged in with " . $ftp_server ;
}
else {
echo "Log in failed" ;
}
ftp_close( $ftp_conn );
?>
|
Output:
Successfully logged in with 8.8.8.8
References: https://www.php.net/manual/en/function.ftp-ssl-connect.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...