Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ftp_nb_fput() function in PHP
The ftp_nb_fput() function uploads data from an open file pointer to a file on the FTP server using non-blocking mode. This allows your script to continue processing while the upload happens in the background.
Syntax
ftp_nb_fput(con, remote_file, open_file, transfer_mode, beg_pos);
Parameters
con − The FTP connection resource
remote_file − The remote file path on the FTP server
open_file − The file pointer resource opened with
fopen()-
transfer_mode − The transfer mode. Possible values:
−
FTP_ASCIIfor text files−
FTP_BINARYfor binary files beg_pos − (Optional) The position to start uploading from
Return Value
The function returns one of the following constants ?
FTP_FAILED − Upload failed
FTP_FINISHED − Upload completed successfully
FTP_MOREDATA − Upload still in progress
Example
Here's how to upload a file using non-blocking mode ?
<?php
$ftp_server = "192.168.0.4";
$ftp_user = "jacob";
$ftp_pass = "tywg61gh";
// Establish connection
$con = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($con, $ftp_user, $ftp_pass);
$my_serverfile = "demo.txt";
$local_file = "new.txt";
// Open local file for reading
$file_pointer = fopen($local_file, "r");
// Start non-blocking upload
$result = ftp_nb_fput($con, $my_serverfile, $file_pointer, FTP_BINARY);
// Continue until upload is complete
while ($result == FTP_MOREDATA) {
// Continue uploading
$result = ftp_nb_continue($con);
echo "Upload in progress...<br>";
}
// Check final result
if ($result != FTP_FINISHED) {
echo "Error uploading the file!";
exit(1);
} else {
echo "File uploaded successfully!";
}
// Clean up
fclose($file_pointer);
ftp_close($con);
?>
Key Points
Use
ftp_nb_continue()to check upload progressAlways close the file pointer with
fclose()Use
FTP_BINARYfor most file types to preserve data integrityThe function is non-blocking, allowing other operations during upload
Conclusion
The ftp_nb_fput() function provides efficient non-blocking file uploads to FTP servers. Use it when you need to upload files while keeping your script responsive for other tasks.
