Skip to content

ftp_nb_put()

Understanding the PHP Function ftp_nb_put()

The ftp_nb_put() function is a built-in PHP function that uploads a file to an FTP server using non-blocking mode. This guide covers its parameters, syntax, usage, and error handling to help you integrate it effectively into your PHP projects.

What is ftp_nb_put()?

The ftp_nb_put() function uploads a file to an FTP server without halting script execution. It accepts three required parameters:

  1. ftp_stream: The connection identifier returned by ftp_connect() (an FTP\Connection object in PHP 7.1+).
  2. remote_file: The remote file path where the file will be uploaded.
  3. local_file: The local file path.

The function returns one of the following constants: FTP_MOREDATA (transfer in progress), FTP_FINISHED (transfer completed), or FTP_FAILED (error occurred).

Syntax of ftp_nb_put()

The syntax of the ftp_nb_put() function is as follows:

Syntax of ftp_nb_put()

php
int ftp_nb_put ( FTP\Connection $ftp_stream , string $remote_file , string $local_file , int $mode [, int $startpos = 0 ] )

The ftp_nb_put() function takes three required parameters (ftp_stream, remote_file, and local_file) and one optional parameter (startpos). The mode parameter is required and specifies the transfer mode: FTP_BINARY is recommended for most files (images, archives, executables) to prevent line-ending corruption, while FTP_ASCII is used for plain text files (though FTP_BINARY is generally safer for all file types). The startpos parameter specifies the position in the remote file to start the upload from. By default, startpos is set to 0, which means the upload will start from the beginning of the file.

Usage of ftp_nb_put()

To use the ftp_nb_put() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:

Usage of ftp_nb_put()

php
<?php

// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');
if (!$conn) {
    die('Could not connect to FTP server.');
}

// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
    die('Login failed.');
}

// Initiate an asynchronous FTP operation
$result = ftp_nb_put($conn, 'remote_file.txt', 'local_file.txt', FTP_BINARY);
if ($result === FTP_FAILED) {
    die('Upload failed.');
}

// Continue the asynchronous FTP operation
while ($result === FTP_MOREDATA) {
    // Do something else while waiting for the FTP operation to complete
    $result = ftp_nb_continue($conn);
}

if ($result === FTP_FINISHED) {
    echo "Upload completed successfully.\n";
}

// Close the FTP connection
ftp_close($conn);

In this example, we establish a connection to the FTP server using the ftp_connect() function. Then we log in using our FTP credentials using the ftp_login() function. We initiate an asynchronous FTP operation using the ftp_nb_put() function. We continue the operation using the ftp_nb_continue() function inside a loop that checks for FTP_MOREDATA, and finally verify completion with FTP_FINISHED before closing the FTP connection.

Error handling in ftp_nb_put()

It's important to handle errors properly when using the ftp_nb_put() function. If the function returns FTP_FAILED, it means that the upload was unsuccessful. Here's an example of how to handle errors:

Error handling in ftp_nb_put()

php
<?php

$result = ftp_nb_put($conn, 'remote_file.txt', 'local_file.txt', FTP_BINARY);

if ($result === FTP_FAILED) {
    echo "Failed to upload file to remote server.\n";
}

while ($result === FTP_MOREDATA) {
    // Do something else while waiting for the FTP operation to complete
    $result = ftp_nb_continue($conn);
}

if ($result === FTP_FINISHED) {
    echo "Upload completed.\n";
}

ftp_close($conn);

By handling errors appropriately and checking the return value of the function, you can ensure the success of your FTP operations using the ftp_nb_put() function.

Conclusion

In conclusion, the ftp_nb_put() function is a useful tool for uploading files to an FTP server using non-blocking mode. It allows you to continue with other operations while waiting for the upload to complete. With proper usage and error handling, this function can be a valuable asset in your PHP projects.

Note: The ftp_nb_* functions are considered legacy in modern PHP. For new projects, consider using cURL or asynchronous HTTP libraries like Guzzle for better performance, security, and broader protocol support.

Practice

What is the purpose of ftp_nb_put function in PHP?

Dual-run preview — compare with live Symfony routes.