PHP | ftp_get() function
The ftp_get() function is an inbuilt function in PHP which is used to get or download files from FTP server to local server or machine.
ftp_get( $ftp_connection, $local_file_path, $server_file_path, $mode_of_file_transfer, $starting_position );
Parameters: This function accepts five parameters as mentioned above and described below:
- $ftp_connection: It is required parameter. It specifies the already existing FTP connection to use for downloading the file from the FTP server.
- $local_file_path: It is required parameter. It specifies the path of local server or machine to which the file is being downloaded.
- $server_file_path: It is required parameter. It specifies path of the file is to be downloaded from the FTP server.
- $mode_of_file_transfer: It is required parameter. It specifies the transfer mode. Values of the parameter is either FTP_ASCII or FTP_BINARY.
- $starting_position: It is an optional parameter. It specifies the position in the remote file to start downloading.
Return Value: It returns True on success or False on failure.
- This function is available for 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.
- Mode should be chosen correctly i.e. either FTP_ASCII or FTP_BINARY
Vital information to run the following code: The following code will not work in online IDE as they don’t allow to interact with files. So try to run on PHP hosting server. Make sure provide correct FTP server, username, password, server file path, and local file path. If the file specifies as server file doesn’t exist then an error will occur so make sure server file exists. In these examples, the file mentioned as $server_file will be downloaded as local file to the relative path as provided in $local_file.
Below examples illustrate the ftp_get() function in PHP:
Example 2: Downloading of binary file from FTP Server.