PHP ftp_get() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_get() downloads a file from the FTP server and saves it to a local file. It is the easiest way to fetch a file over FTP.

You give it a local path to save to and the remote path to download. Choose FTP_ASCII for text files and FTP_BINARY for everything else, such as images, PDFs and ZIP files.

Syntax

Call ftp_get() with the values you need. Start with this line.

PHP
$ok = ftp_get($ftp, "local.txt", "remote.txt", FTP_ASCII);
CallWhat it does
ftp_get($ftp, "local.txt", "remote.txt", FTP_ASCII)Downloads a text file and saves it as local.txt.
ftp_get($ftp, "photo.jpg", "images/photo.jpg", FTP_BINARY)Downloads an image without changing any bytes.
ftp_get($ftp, "big.zip", "big.zip", FTP_BINARY, 1024)Starts downloading from byte 1024 to resume a transfer.

From PHP 8.1 the first argument is an FTP\Connection object. If the local file already exists it is overwritten. The default mode is FTP_BINARY, which is the safe choice for most files.

Library declarations
PHP
ftp_get(FTP\Connection $ftp, string $local_filename, string $remote_filename, int $mode = FTP_BINARY, int $offset = 0): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
local_filenameRequired. The path on your computer where the downloaded file will be saved.
remote_filenameRequired. The path of the file on the FTP server to download.
modeOptional. The transfer mode, FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.
offsetOptional. The position in the remote file to start downloading from. The default is 0.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Download a File

Fetch a text file from the server and save it locally. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

if (ftp_get($ftp, "local-notes.txt", "notes.txt", FTP_ASCII)) {
    echo "Saved to local-notes.txt", PHP_EOL;
} else {
    echo "Download failed.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We connect and log in to the server.
  2. ftp_get() downloads notes. txt and saves it as local-notes.txt.
  3. FTP_ASCII is used because it is a plain text file.
  4. A message is printed based on the true or false result.

Example 2 — Remote File Is Missing

If the remote file does not exist, ftp_get() returns false and raises a warning. Check the result before using the local file.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$ok = @ftp_get($ftp, "missing-copy.txt", "missing.txt", FTP_ASCII);

var_dump($ok);
echo file_exists("missing-copy.txt") ? "File created" : "No local file", PHP_EOL;

ftp_close($ftp);

How It Works

  1. The server has no file called missing. txt.
  2. ftp_get() returns false, and the @ operator hides the warning.
  3. No new local file is kept, so file_exists() returns false.
  4. This shows why you should always check the return value.

Example 3 — Download Every File in a Folder

Combine ftp_nlist() and ftp_get() to copy all files from a remote folder.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$files = ftp_nlist($ftp, "/reports");

foreach ($files as $path) {
    $name = basename($path);

    if (ftp_get($ftp, "downloads_" . $name, $path, FTP_BINARY)) {
        echo "Downloaded ", $name, PHP_EOL;
    }
}

ftp_close($ftp);

How It Works

  1. ftp_nlist() returns an array with the paths of the files in /reports.
  2. basename() extracts just the file name from each path.
  3. ftp_get() downloads every file in binary mode with a prefix for the local name.
  4. A line is printed for each successful download.

Frequently Asked Questions

It downloads a file from an FTP server and saves it to a local path. It returns true on success and false on failure.
ftp_get() saves to a local file path. ftp_fget() writes into an open file handle, which is useful for memory streams.
Use FTP_ASCII for plain text files and FTP_BINARY for everything else. When in doubt, use FTP_BINARY, which is also the default.
Yes. If the local file already exists it is replaced. Check with file_exists() first if you want to avoid that.
Use ftp_nb_get() and ftp_nb_continue(). They download in small steps so your script can do other work in between.

Next: ftp_get_option()

ftp_get_option() reads one of the runtime settings of an open FTP connection. It tells you.

ftp_get_option() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful