Syntax:ftp_get ($ftp_connection, $local_file_path, $server_file_path, $mode_of_file_transfer, $starting_position);
Parameters: This function takes five parameters, as above and described below:
- $ftp_connection:Required. It specifies an existing FTP connection that will be used to download the file from the FTP server.
- $local_file_path:Required. It specifies the path to the local server or machine where the file is uploaded.
- $server_file_path:Required. It specifies the path to the file to be downloaded from the FTP server.
- $mode_of_file_transfer:Required. Indicates the transmission mode. Parameter values - either FTP_ASCII or FTP_BINARY.
- $start_position:is optional. It specifies the position in the remote file to start downloading.
Return Value:Returns True on success or False on failure.
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 mode must be correct, i.e. either FTP_ASCII or FTP_BINARY
Important information to run the following code:The following code will not work in the online IDE because it does not allow file interactions. So try running a PHP server on your hosting. Make sure the correct FTP server, username, password, server file path, and local file path are specified. If the file indicates that the server file does not exist, an error will occur, so make sure the server file exists. In these examples, the file specified as $server_file will be loaded as a local file at the relative path specified in $local_file.The following are examples to illustrate the ftp_get() function in PHP:
Example 1:
// Connect to the FTP server
// Using the correct FTP server
$ftp_server
=
"localhost"
;
// Use correct ftp username
$ftp_username
=
"user"
;
// Use correct FTP password matching
// to the ftp username
$ftp_userpass
=
"user"
;
// Setting up 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!"
;
// Login to the established connection
// with ftp username password
$login
= ftp_login (
$ftp_connection
,
$ftp_username
,
$ftp_userpass
);
if
(
$login
) {
// Success check login
// or not
echo
"
logged in successfully!"
;
// Local file name or path to
// where the file to download
$local_file
=
"local_file.txt"
;
// Server file name or path to
// be loaded
$server_file
=
"server_file.txt"
;
// Download the specified server file
if
(ftp_get (
$ftp_connection
,
$local_file
,
$server_file
, FTP_BINARY)) {
echo
"
Successfully downloaded"
.
"from $server_file to $local_file."
;
}
else
{
echo
"
Error while downloading from"
.
"$server_file to $local_file."
;
}
}
else
{
echo
"
login failed! "
;
}
// echo ftp_get_option ($ftp_connection, 1);
// Close the connection
if
(ftp_close (
$ftp_connection
)) {
echo
"
Connection closed Successfully!"
;
}
}
?>
Output: Example 2Download a binary file from an FTP server.
// Connect to the FTP server
// Using the correct FTP server
$ftp_server
=
"localhost"
;
// Use correct ftp username
$ftp_username
=
"user"
;
// Use correct FTP password matching
// to the ftp username
$ftp_userpass
=
"user"
;
// Setting up ftp connection
$ftp_connection
= ftp_connect (
$ftp_server
, 21)
or
die
(
"Could not connect to $ftp_server"
);
if
(
$ftp_connection
) {
echo
"successfully connected to the ftp server!"
;
// Login to the established connection
// with ftp username password
$login
= ftp_login (
$ftp_connection
,
$ftp_username
,
$ftp_userpass
);
if
(
$login
) {
// Success check login
// or not
echo
"
logged in successfully!"
;
// Local file name or path to
// where the file to download
$local_file
=
"local_file_shiva.jpg"
;
// Server file name or path to
// be loaded
$server_file
=
"shiva.jpg"
;
// Download the specified server file
if
(ftp_get (
$ftp_connection
,
$local_file
,
$server_file
, FTP_BINARY)) {
echo
"
Successfully downloaded from"
.
"$server_file to $local_file."
;
}
else
{
echo
"
Error while downloading from"
.
"$server_file to $local_file."
;
}
}
else
{
echo
"
login failed! "
;
}
// echo ftp_get_option ($ftp_connection, 1);
// Close the connection
if
(ftp_close (
$ftp_connection
)) {
echo
"
Connection closed Successfully!"
;
}
}
?>
Output: Link: https://www.php.net/manual/en/ function.ftp-get.php