PHP ftp_fget() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_fget() downloads a file from the FTP server and writes it into an open local file handle. It is the stream version of ftp_get().

Instead of giving a local file name, you open a file or memory stream yourself and pass the handle. This lets you save to php://temp, php://memory, or any other stream you like.

Syntax

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

PHP
$ok = ftp_fget($ftp, $handle, "remote.txt", FTP_ASCII, 0);
CallWhat it does
ftp_fget($ftp, $handle, "remote.txt", FTP_ASCII)Downloads a text file into the open handle.
ftp_fget($ftp, $handle, "photo.jpg", FTP_BINARY)Downloads an image without changing any bytes.
ftp_fget($ftp, $handle, "big.zip", FTP_BINARY, 1024)Starts reading the remote file from byte 1024 (resume position).

The handle must be opened for writing, for example with fopen('file.txt', 'w'). From PHP 8.1 the first argument is an FTP\Connection object. After the call, the handle is not rewound, so use rewind() before you read it back.

Library declarations
PHP
ftp_fget(FTP\Connection $ftp, resource $stream, 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().
streamRequired. An open, writable file pointer where the downloaded data is written.
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 Into a Local File Handle

Open a local file for writing and let ftp_fget() fill it. The host and login are placeholders.

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

$handle = fopen("local-notes.txt", "w");

if (ftp_fget($ftp, $handle, "notes.txt", FTP_ASCII)) {
    echo "Downloaded notes.txt", PHP_EOL;
}

fclose($handle);
ftp_close($ftp);

How It Works

  1. fopen() with mode w opens (or creates) local-notes. txt for writing.
  2. ftp_fget() downloads notes. txt from the server into that handle.
  3. A true result means all data was written.
  4. fclose() closes the local file, and ftp_close() ends the session.

Example 2 — Download Into Memory

Use php://temp to keep the file in memory instead of saving it to disk. Remember to rewind before reading.

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

$handle = fopen("php://temp", "r+");

ftp_fget($ftp, $handle, "greeting.txt", FTP_ASCII);

rewind($handle);
$content = stream_get_contents($handle);

echo "Content: ", trim($content), PHP_EOL;

fclose($handle);
ftp_close($ftp);

How It Works

  1. php://temp is a temporary in-memory stream that is also readable.
  2. ftp_fget() writes the downloaded text into it.
  3. rewind() moves the pointer back to the start so we can read what was written.
  4. stream_get_contents() returns the full text, which we print.

Example 3 — Check the Result Before Using the File

Only keep the local file if the download worked, and delete the empty file if it failed.

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

$local = "download.zip";
$handle = fopen($local, "w");

$ok = @ftp_fget($ftp, $handle, "archive.zip", FTP_BINARY);
fclose($handle);

if ($ok) {
    echo "Saved ", filesize($local), " bytes", PHP_EOL;
} else {
    unlink($local);
    echo "Download failed, removed empty file.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We open the local file and run ftp_fget() in binary mode.
  2. The handle is closed right after the transfer so everything is flushed to disk.
  3. On success, filesize() reports how many bytes were saved.
  4. On failure, unlink() removes the empty file so we do not keep a broken copy.

Frequently Asked Questions

It downloads a remote file from an FTP server and writes it into an open local file handle that you provide.
ftp_get() takes a local file name and saves the file for you. ftp_fget() takes an open file handle, which gives you more control, for example saving to memory.
Yes, if you want to read the data back. After the download the pointer is at the end, so call rewind() first.
Use FTP_ASCII for plain text and FTP_BINARY for everything else, such as images, PDFs and ZIP files.
It tells the server which byte to start from in the remote file. It is useful for resuming an interrupted download.

Next: ftp_fput()

ftp_fput() uploads data from an open local file handle to a file on the FTP server. It is .

ftp_fput() 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