PHP | ftp_ssl_connect() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Use ftp_ssl_connect() function to
// check Connection
$conn = ftp_ssl_connect("ftp.testftp.com"
    or die("Could not connect");
  
echo "Connection established successfully";
  
?>

chevron_right


Output:

Connection established successfully

Example 2: This example uses ftp_ssl_connect() function to logging in using SSL-FTP Connection

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Setting up basic SSL connection
  
// User IP address to which you
// want to connect to
$ftp_server = "8.8.8.8";
  
// Logging in in the established ftp connection.
$ftp_conn = ftp_ssl_connect($ftp_server)
    or die("Could not connect to $ftp_server");
  
// Use your username
$ftp_username = "your_username";
  
// Use your password
$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";
}
  
// Closeing SSL connection
ftp_close($ftp_conn);
  
?>

chevron_right


Output:

Successfully logged in with 8.8.8.8

References: https://www.php.net/manual/en/function.ftp-ssl-connect.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.