Функция php ftp_nb_fput()— это неблокирующая версия функции ftp_fput()в PHP. Он позволяет асинхронно загружать файл на удаленный FTP-сервер, то есть сценарий может продолжать выполнение во время передачи файла. Вот пример использования функции ftp_nb_fput():
<?php
// Connect to the FTP server
$ftpServer = 'ftp.example.com';
$ftpUsername = 'your_username';
$ftpPassword = 'your_password';
$connId = ftp_connect($ftpServer);
ftp_login($connId, $ftpUsername, $ftpPassword);
// Open the local file for reading
$localFile = 'path/to/local/file.txt';
$handle = fopen($localFile, 'r');
// Define the remote file path
$remoteFile = '/path/to/remote/file.txt';
// Start the non-blocking upload
$ret = ftp_nb_fput($connId, $remoteFile, $handle, FTP_BINARY);
// Check if the upload has started successfully
if ($ret == FTP_FAILED) {
echo 'Failed to start the upload.';
} else {
// Continue executing the script while the upload is in progress
while ($ret == FTP_MOREDATA) {
// Do some other tasks here if needed
// ...
// Continue the upload
$ret = ftp_nb_continue($connId);
}
// Check the final status of the upload
if ($ret == FTP_FINISHED) {
echo 'File uploaded successfully.';
} else {
echo 'Failed to upload the file.';
}
}
// Close the FTP connection and the local file handle
ftp_close($connId);
fclose($handle);
?>
В этом примере мы сначала подключаемся к FTP-серверу, используя ftp_connect()и ftp_login(). Затем мы открываем локальный файл с помощью fopen()и определяем путь к удаленному файлу. Мы инициируем неблокирующую загрузку с помощью ftp_nb_fput(), а затем используем цикл для продолжения загрузки до ее завершения с помощью ftp_nb_continue(). Наконец, мы проверяем статус загрузки и закрываем FTP-соединение и дескриптор локального файла.