PHP ftp_nb_fput() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_nb_fput() starts a non-blocking upload of data from an open file pointer (a stream) to a file on the FTP server.

It is the non-blocking version of ftp_fput(). It uploads the first chunk and returns right away, then you call ftp_nb_continue() until the upload is done. This keeps your script responsive during big uploads.

Syntax

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

PHP
$status = ftp_nb_fput($ftp, 'remote.zip', $handle, FTP_BINARY);
CallWhat it does
ftp_nb_fput($ftp, 'remote.zip', $handle)Uploads the stream to remote.zip in binary mode.
ftp_nb_fput($ftp, 'notes.txt', $handle, FTP_ASCII)Uploads a text file using ASCII mode.
ftp_nb_fput($ftp, 'big.iso', $handle, FTP_BINARY, 2048)Resumes an upload from byte 2048 of the remote file.

Open the local file with fopen() in read mode first. The function returns FTP_MOREDATA, FTP_FINISHED or FTP_FAILED. Keep calling ftp_nb_continue() while the status is FTP_MOREDATA.

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

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
remote_filenameRequired. The path of the file to create on the FTP server.
streamRequired. An open file pointer. PHP reads from it until the end of the file.
modeOptional. The transfer mode, FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.
offsetOptional. The position in the remote file where the upload starts. The default is 0.

Return Value

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

PHP Examples

Example 1 — Upload From an Open Stream

Open a local file, start the upload 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('photo.jpg', 'rb');

$status = ftp_nb_fput($ftp, 'photos/photo.jpg', $handle, FTP_BINARY);

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

fclose($handle);
echo $status === FTP_FINISHED ? "Upload complete" : "Upload failed", PHP_EOL;

ftp_close($ftp);

How It Works

  1. fopen() with rb opens the local image for reading in binary mode.
  2. ftp_nb_fput() sends the first chunk to the remote path.
  3. ftp_nb_continue() keeps sending the rest while the status is FTP_MOREDATA.
  4. fclose() closes the local file after the transfer is over.

Example 2 — Upload In-Memory Data

Because it accepts any stream, you can upload text that never existed as a file on disk.

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

$handle = fopen('php://memory', 'r+');
fwrite($handle, "Hello from CodeToFun!\n");
rewind($handle);

$status = ftp_nb_fput($ftp, 'hello.txt', $handle, FTP_ASCII);

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

fclose($handle);
echo $status === FTP_FINISHED ? "hello.txt created" : "Upload failed", PHP_EOL;

ftp_close($ftp);

How It Works

  1. php://memory creates a temporary stream that lives only in RAM.
  2. fwrite() puts the text in it, and rewind() moves the pointer back to the start.
  3. ftp_nb_fput() uploads the stream as hello. txt using ASCII mode.
  4. The loop completes the upload, and the final message confirms it.

Example 3 — Resume an Interrupted Upload

Use the size of the remote file as the offset and continue from there.

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

$remote = 'backup.zip';
$size = ftp_size($ftp, $remote);
$offset = $size > 0 ? $size : 0;

$handle = fopen('backup.zip', 'rb');
fseek($handle, $offset);

$status = ftp_nb_fput($ftp, $remote, $handle, FTP_BINARY, $offset);

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

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

ftp_close($ftp);

How It Works

  1. ftp_size() reports how many bytes already exist on the server, or -1 if the file is missing.
  2. fseek() moves the local pointer to the same position.
  3. The same offset is passed to ftp_nb_fput() so the data lines up.
  4. ftp_nb_continue() uploads the remaining bytes.

Frequently Asked Questions

It starts a non-blocking upload of data from an open file pointer to a remote FTP file. You complete the upload with ftp_nb_continue().
ftp_nb_fput() uploads from a stream you opened, while ftp_nb_put() takes a local file name and opens the file itself.
FTP_MOREDATA means keep calling ftp_nb_continue(). FTP_FINISHED means the upload is complete. FTP_FAILED means an error stopped it.
Yes. Write the string to a php://memory or php://temp stream, call rewind(), and pass that stream to ftp_nb_fput().
Only after the status is FTP_FINISHED or FTP_FAILED. Closing it earlier interrupts the upload.

Next: ftp_nb_get()

ftp_nb_get() starts a non-blocking download of a remote file and saves it to a local file .

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