FTP Function in PHP

Introduction

I will explain the ftp_connect() and ftp_close() functions in PHP. I will first describe the ftp_connect() function. The ftp_connect() function opens an FTP connection. This example is showm in the following.

Syntax

ftp_connect(host, port, timeout);

 

Parameter          Description
host This parameter should not have any trailing slashes and should not be prefixed with FTP.
port This parameter specifies an alternate port to connect to FTP. If it is omitted or set to zero, then the default FTP port, 21, will be used.
timeout Specifies the timeout for all subsequent network operations and the default time of FTP is 90 seconds.

Returns a FTP connection on success and otherwise an error.

Example

<?php 

$ftp_conn="www.c-sharpcorner.com"

$conn_id = ftp_connect($ftp_conn) or die("Couldn't connect to $ftp_server");  

if (!ftp_connect($ftp_conn)) 

echo "not connected"

else 

echo "successful connected"

?> 

Output

connetion.jpg

The function I will explain next is ftp_close(). This function closes an FTP connection or given link. Let's describe this function in the following.

Syntax

ftp_close(ftp_conn);

 

Parameter Description
ftp_conn  The link identifier of the FTP connection.

Returns true on success and otherwise an error.

Note: After using this function, you can no longer use the FTP connection and must create a new connection with the ftp_connect() function.

Example

<?php

$conn_id = ftp_connect($ftp_server);

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

echo ftp_pwd($conn_id);

ftp_close($conn_id);

?>


Similar Articles