Content · Web Development · PHP

PHP ftp_nb_fget() Function: Usage and Examples

Begins a nonblocking download into a local opened file.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Begins a nonblocking download into a local opened file stream. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.

What It Does


Begins a nonblocking download into a local opened file stream.

Syntax and Parameters


The examples below show a real call to ftp_nb_fget with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.

Requirements: FTP extension and a controlled test FTP/FTPS server; ftp_ssl_connect does not validate the server certificate.

Compatibility and security: Important: ftp_ssl_connect does not verify the server certificate; it is unsuitable alone for authenticating a remote FTPS server. Prefer a transport implementation with peer verification (or an approved SFTP client) for untrusted networks. FTP and SFTP are different protocols.

These examples are for an isolated development environment. Hostnames, credentials and remote paths must be replaced with your approved configuration.

Example 1: Core Behavior


Configure a test FTPS server and a dedicated low-privilege account before running this example.

<?php
$host = getenv('FTP_HOST'); $user = getenv('FTP_USER'); $pass = getenv('FTP_PASSWORD');
if (!$host || !$user || !$pass) { throw new RuntimeException('Configure FTP_HOST, FTP_USER and FTP_PASSWORD'); }
// ftp_ssl_connect does not verify the peer certificate; use only on a trusted test server.
$ftp = ftp_ssl_connect($host, 21, 8);
if (!$ftp || !ftp_login($ftp, $user, $pass)) { throw new RuntimeException('FTPS connection/login failed'); }
ftp_pasv($ftp, true);
$stream = fopen('php://temp', 'w+b');
if ($stream === false) throw new RuntimeException('Cannot open stream');
$status = ftp_nb_fget($ftp, $stream, '/exports/report.csv', FTP_BINARY);
while ($status === FTP_MOREDATA) { $status = ftp_nb_continue($ftp); }
echo $status === FTP_FINISHED ? "Completed" : "Transfer failed";
fclose($stream);
ftp_close($ftp);

Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.

Example 2: Alternative or Failure Case


Repeat with an account limited to the correct directory and check the returned status.

<?php
$host = getenv('FTP_HOST'); $user = getenv('FTP_USER'); $pass = getenv('FTP_PASSWORD');
if (!$host || !$user || !$pass) { throw new RuntimeException('Configure FTP_HOST, FTP_USER and FTP_PASSWORD'); }
// ftp_ssl_connect does not verify the peer certificate; use only on a trusted test server.
$ftp = ftp_ssl_connect($host, 21, 9);
if (!$ftp || !ftp_login($ftp, $user, $pass)) { throw new RuntimeException('FTPS connection/login failed'); }
ftp_pasv($ftp, true);
$stream = fopen('php://temp', 'w+b');
if ($stream === false) throw new RuntimeException('Cannot open stream');
$status = ftp_nb_fget($ftp, $stream, '/exports/weekly.csv', FTP_BINARY);
while ($status === FTP_MOREDATA) { $status = ftp_nb_continue($ftp); }
echo $status === FTP_FINISHED ? "Completed" : "Transfer failed";
fclose($stream);
ftp_close($ftp);

Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.

Practical Example: Applying the Rule


A production workflow must also validate remote paths and handle errors without exposing credentials.

<?php
$host = getenv('FTP_HOST'); $user = getenv('FTP_USER'); $pass = getenv('FTP_PASSWORD');
if (!$host || !$user || !$pass) { throw new RuntimeException('Configure FTP_HOST, FTP_USER and FTP_PASSWORD'); }
// ftp_ssl_connect does not verify the peer certificate; use only on a trusted test server.
$ftp = ftp_ssl_connect($host, 21, 10);
if (!$ftp || !ftp_login($ftp, $user, $pass)) { throw new RuntimeException('FTPS connection/login failed'); }
ftp_pasv($ftp, true);
$stream = fopen('php://temp', 'w+b');
if ($stream === false) throw new RuntimeException('Cannot open stream');
$status = ftp_nb_fget($ftp, $stream, '/exports/archive.csv', FTP_BINARY);
while ($status === FTP_MOREDATA) { $status = ftp_nb_continue($ftp); }
echo $status === FTP_FINISHED ? "Completed" : "Transfer failed";
fclose($stream);
ftp_close($ftp);

In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.

Important Behavior and Edge Cases


Keep the local stream open until ftp_nb_continue reports completion.

Check both authentication and operation results; FTP server capabilities and directory permissions vary. PHP ftp_ssl_connect does not verify TLS peer certificates, so the examples are limited to trusted test environments and are not a secure Internet-facing FTPS implementation.

Comparison with Related Functions or Patterns


ApproachWhen it differs
ftp_nb_fgetBegins a nonblocking download into a local opened file stream.
Related API or patternftp_fget uses a blocking download.

Common Beginner Mistakes


MistakeCorrect approach
Mistaking FTP_MOREDATA for a completed transferKeep the local stream open until ftp_nb_continue reports completion.
Substituting a related API without checking its semanticsftp_fget uses a blocking download.

Key Takeaways


  • Begins a nonblocking download into a local opened file stream.
  • Keep the local stream open until ftp_nb_continue reports completion.
  • ftp_fget uses a blocking download.

Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.

Share this resourceSend this page to someone who may find it useful.