Download a file from the FTP server and save it to a local file.
<?php // connect and login to FTP server $ftp_server = “ftp.example.com”; $ftp_conn = ftp_connect($ftp_server) or die(“Could not connect to $ftp_server”); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass); $local_file = “local.zip”; $server_file = “server.zip”; // download server file if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) { echo “Successfully written to $local_file.”; } else { echo “Error downloading $server_file.”; } // close connection ftp_close($ftp_conn); ?> |
The ftp_get() function downloads a file from the FTP server and saves it to a local file.
ftp_get(ftp_conn, local_file, server_file, mode, startpos); |
Parameter |
Description |
ftp_conn |
Mandatory. Specifies the FTP connection to use. |
local_file |
Mandatory. Specifies the local file path (will be overwritten if the file already exists). |
server_file |
Mandatory. Specifies the file on the server to download. |
mode |
Optional. Specifies the transfer mode, with possible values being FTP_ASCII or FTP_BINARY. |
startpos |
Optional. Specifies the starting position in the remote file for downloading. |
Return Value: |
Optional. Specifies the starting position in the remote file for downloading. |
PHP Version: |
4+ |
PHP Changelog: |
In PHP 7.3, the The |