How To Download File On The FTP Server Using PHP

In this article, we will see how to download files from the FTP server using PHP. Many times we have requirements to retrieve files from the FTP server. So, here I will you file download in FTP using FTP get function. ftp_get() function is used to download file from the FTP server.

 So, let's see the retrieved file from the FTP server, download the file from the FTP server, the FTP server download file laravel 8, laravel file download from the FTP server,  ftp_get function() PHP.

Syntax:

The ftp_get() function gets (downloads) a file from the FTP server, and saves it into a local file.

ftp_fget(ftp_conn, open_file, server_file, mode, startpos);

 

Parameters:

ftp_conn - ftp_conn is a required parameter and it is used to specify the FTP connection.

open_file - open_file is a required parameter and it is used to specify an open local file in which we store the data.

server_file - local_file is a required parameter and it is used to specify the server file to download.

mode - mode is an optional parameter and it is used to specify the transfer mode. It has 2 possible values: 1) FTP_ASCII and 2) FTP_BINARY.

startpos - startpos is an optional parameter and it is used to specify the position in the remote file to start downloading from.

 

 

Example :

The ftp_get() function retrieves a remote file from the FTP server and saves it into an open local file.

<?php
// connect to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

// login to FTP server
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$server_file = "somefile.txt";

// open local file to write to
$local_file = "local.txt";
$fp = fopen($local_file,"w");

// download server file and save it to open local file
if (ftp_fget($ftp_conn, $fp, $server_file, FTP_ASCII, 0))
  {
  echo "Successfully written to $local_file.";
  }
else
  {
  echo "Error downloading $server_file.";
  }

// close connection and file handler
ftp_close($ftp_conn);
fclose($fp);
?>

 


You might also like :

RECOMMENDED POSTS

FEATURE POSTS