What are the potential issues with using ftp_nb_get for implementing a progress bar in PHP?

Using ftp_nb_get for implementing a progress bar in PHP may be challenging because it does not provide a direct way to track the progress of the file transfer. To solve this, you can use ftp_size to get the total file size and then track the progress based on the bytes received.

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_user, $ftp_pass);

// Get file size
$file_size = ftp_size($conn_id, 'remote_file.txt');

// Download file and track progress
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
$handle = fopen($local_file, 'w');
$bytes_total = 0;
$bytes_received = 0;

ftp_nb_get($conn_id, $local_file, $remote_file, FTP_BINARY);

while ($bytes_received < $file_size) {
    $bytes_received = filesize($local_file);
    $progress = ($bytes_received / $file_size) * 100;
    echo "Progress: " . round($progress, 2) . "%\n";
    usleep(1000000); // Wait for a second
}

// Close FTP connection
ftp_close($conn_id);