PHP ftp_nb_fget() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_nb_fget() starts a non-blocking download of a remote file and writes it into an open file pointer (a stream) that you provide.

It works like ftp_fget(), but it does not wait for the whole download to finish. It returns after the first chunk, and you call ftp_nb_continue() to receive the rest. This is useful for large files or when you want to show progress.

Syntax

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

PHP
$status = ftp_nb_fget($ftp, $handle, 'remote.zip', FTP_BINARY);
CallWhat it does
ftp_nb_fget($ftp, $handle, 'remote.zip')Starts a binary download of remote.zip into $handle.
ftp_nb_fget($ftp, $handle, 'notes.txt', FTP_ASCII)Downloads a text file using ASCII mode.
ftp_nb_fget($ftp, $handle, 'big.iso', FTP_BINARY, 1024)Resumes a download starting at byte 1024 of the remote file.

It returns FTP_MOREDATA, FTP_FINISHED or FTP_FAILED. If the status is FTP_MOREDATA, call ftp_nb_continue() until the transfer finishes. Open the local stream with fopen() in write mode first, and close it with fclose() afterwards.

Library declarations
PHP
ftp_nb_fget(FTP\Connection $ftp, resource $stream, string $remote_filename, int $mode = FTP_BINARY, int $offset = 0): int

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
streamRequired. An open file pointer where the downloaded data is written.
remote_filenameRequired. The path of the file on the FTP server.
modeOptional. The transfer mode, FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.
offsetOptional. The position in the remote file where the download starts. The default is 0.

Return Value

Return ValueDescription
intFTP_MOREDATA if the transfer is still running, FTP_FINISHED when it is done, or FTP_FAILED on error.

PHP Examples

Example 1 — Download Into an Open Stream

Open a local file, start the download and finish it with ftp_nb_continue().

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
ftp_pasv($ftp, true);

$handle = fopen('downloaded.zip', 'wb');

$status = ftp_nb_fget($ftp, $handle, 'remote.zip', FTP_BINARY);

while ($status === FTP_MOREDATA) {
    $status = ftp_nb_continue($ftp);
}

fclose($handle);
echo $status === FTP_FINISHED ? "Saved downloaded.zip" : "Download failed", PHP_EOL;

ftp_close($ftp);

How It Works

  1. fopen() with the wb mode creates a local file ready for binary writing.
  2. ftp_nb_fget() starts the download and returns a status.
  3. The loop keeps calling ftp_nb_continue() while data is still arriving.
  4. fclose() closes the local file once the transfer has ended.

Example 2 — Handle a Missing Remote File

If the remote file does not exist, the status is FTP_FAILED. Remove the empty local file in that case.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
ftp_pasv($ftp, true);

$local = 'missing.txt';
$handle = fopen($local, 'wb');

$status = @ftp_nb_fget($ftp, $handle, 'no-such-file.txt', FTP_ASCII);

while ($status === FTP_MOREDATA) {
    $status = ftp_nb_continue($ftp);
}

fclose($handle);

if ($status === FTP_FAILED) {
    unlink($local);
    echo "Remote file not found", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The local file is created before the download starts.
  2. ftp_nb_fget() returns FTP_FAILED because the server has no such file.
  3. The while loop is skipped since the status is not FTP_MOREDATA.
  4. unlink() deletes the empty local file so no clutter is left behind.

Example 3 — Resume a Partial Download

Use the offset parameter to continue a download from where a previous attempt stopped.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
ftp_pasv($ftp, true);

$local  = 'big.iso';
$offset = file_exists($local) ? filesize($local) : 0;

$handle = fopen($local, 'ab');

$status = ftp_nb_fget($ftp, $handle, 'big.iso', FTP_BINARY, $offset);

while ($status === FTP_MOREDATA) {
    $status = ftp_nb_continue($ftp);
}

fclose($handle);
echo "Resumed from byte {$offset}", PHP_EOL;

ftp_close($ftp);

How It Works

  1. filesize() tells you how many bytes were already saved.
  2. fopen() with the ab mode appends new data to the end of the file.
  3. The offset argument tells the server to start sending from that byte.
  4. The loop finishes the rest of the file using ftp_nb_continue().

Frequently Asked Questions

It starts a non-blocking download of a remote file and writes the data into an open file pointer. You finish the download with ftp_nb_continue().
ftp_nb_fget() writes to a file pointer you already opened, while ftp_nb_get() takes a local file name and opens the file for you.
It means the transfer is still in progress. Call ftp_nb_continue() again until you receive FTP_FINISHED or FTP_FAILED.
Use FTP_BINARY for images, archives and most files. Use FTP_ASCII only for plain text files where line endings may need converting.
Yes. PHP does not close the file pointer for you, so call fclose() after the transfer has finished or failed.

Next: ftp_nb_fput()

ftp_nb_fput() starts a non-blocking upload of data from an open file pointer (a stream) to.

ftp_nb_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