PHP ftp_nb_continue() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_nb_continue() continues a non-blocking FTP transfer that you started with ftp_nb_get(), ftp_nb_put(), ftp_nb_fget() or ftp_nb_fput().

A non-blocking transfer does not finish in one call. It sends a small chunk and returns, so your script can do other work in between. You keep calling ftp_nb_continue() until it says the transfer is done or has failed.

Syntax

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

PHP
$status = ftp_nb_continue($ftp);
CallWhat it does
ftp_nb_continue($ftp)Sends or receives the next chunk and returns the current status.
while (ftp_nb_continue($ftp) === FTP_MOREDATA) {}Keeps going until the transfer is no longer in progress.
$status === FTP_FINISHEDChecks that the whole transfer completed successfully.

It returns one of three constants: FTP_MOREDATA (still working, call again), FTP_FINISHED (done) or FTP_FAILED (something went wrong). Only one non-blocking transfer can run on a connection at a time.

Library declarations
PHP
ftp_nb_continue(FTP\Connection $ftp): int

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object that has an unfinished non-blocking transfer.

Return Value

Return ValueDescription
intFTP_MOREDATA while the transfer is in progress, FTP_FINISHED when it is complete, or FTP_FAILED on error.

PHP Examples

Example 1 — Finish a Non-Blocking Download

Start a download with ftp_nb_get() and keep calling ftp_nb_continue() until it ends.

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

$status = ftp_nb_get($ftp, 'local.zip', 'remote.zip', FTP_BINARY);

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

echo $status === FTP_FINISHED ? "Download complete" : "Download failed", PHP_EOL;

ftp_close($ftp);

How It Works

  1. ftp_nb_get() begins the download and returns FTP_MOREDATA if there is more to fetch.
  2. The while loop calls ftp_nb_continue() again and again.
  3. The loop ends when the status is no longer FTP_MOREDATA.
  4. The final check tells you if it ended with FTP_FINISHED or FTP_FAILED.

Example 2 — Detect a Failed Transfer

A failed transfer returns FTP_FAILED. Handle all three statuses to be safe.

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

$status = ftp_nb_put($ftp, 'remote/big.iso', 'big.iso', FTP_BINARY);

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

switch ($status) {
    case FTP_FINISHED:
        echo "Upload complete", PHP_EOL;
        break;
    case FTP_FAILED:
        echo "Upload failed", PHP_EOL;
        break;
}

ftp_close($ftp);

How It Works

  1. ftp_nb_put() starts an upload to a remote folder that does not allow writing.
  2. The loop only continues while the status is FTP_MOREDATA.
  3. The server rejects the data, so the status becomes FTP_FAILED.
  4. The switch prints a message that matches the final status.

Example 3 — Do Other Work Between Chunks

The main benefit of non-blocking mode is that your script can do other things between chunks, such as show progress.

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

$status = ftp_nb_get($ftp, 'video.mp4', 'video.mp4', FTP_BINARY);
$chunks = 0;

while ($status === FTP_MOREDATA) {
    $chunks++;
    if ($chunks % 100 === 0) {
        echo "Still downloading... ({$chunks} chunks)", PHP_EOL;
    }
    $status = ftp_nb_continue($ftp);
}

echo "Done after {$chunks} chunks", PHP_EOL;

ftp_close($ftp);

How It Works

  1. The counter $chunks tracks how many times the loop ran.
  2. Every 100 chunks the script prints a small progress message.
  3. ftp_nb_continue() moves the transfer forward on every pass.
  4. When the status stops being FTP_MOREDATA, the loop ends and the total is shown.

Frequently Asked Questions

It continues a non-blocking FTP transfer started by ftp_nb_get(), ftp_nb_put(), ftp_nb_fget() or ftp_nb_fput().
It returns FTP_MOREDATA while the transfer is running, FTP_FINISHED when it is complete and FTP_FAILED if an error happens.
They let your script do other work, like updating a progress message, while a large file is being transferred in small steps.
No. A connection supports only one non-blocking transfer at a time. Finish the first one or open a second connection.
The transfer stays unfinished and the file will be incomplete. Always loop until you get FTP_FINISHED or FTP_FAILED.

Next: ftp_nb_fget()

ftp_nb_fget() starts a non-blocking download of a remote file and writes it into an open f.

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