PHP: Hypertext Preprocessor (original) (raw)

ftp_nb_fput

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

ftp_nb_fput — Stores a file from an open file to the FTP server (non-blocking)

Description

The difference between this function and the ftp_fput() is that this function uploads the file asynchronously, so your program can perform other operations while the file is being uploaded.

Parameters

ftp

An FTP\Connection instance.

remote_filename

The remote file path.

stream

An open file pointer on the local file. Reading stops at end of file.

mode

The transfer mode. Must be either [FTP_ASCII](ftp.constants.php#constant.ftp-ascii) or**[FTP_BINARY](ftp.constants.php#constant.ftp-binary)**.

offset

The position in the remote file to start uploading to.

Changelog

Version Description
8.1.0 The ftp parameter expects an FTP\Connection instance now; previously, a resource was expected.
7.3.0 The mode parameter is now optional. Formerly it has been mandatory.

Examples

Example #1 ftp_nb_fput() example

`<?php

$file

= 'index.php';$fp = fopen($file, 'r');$ftp = ftp_connect($ftp_server);$login_result = ftp_login($ftp, ftpusername,ftp_user_name, ftpusername,ftp_user_pass);// Initiate the upload ret=ftpnbfput(ret = ftp_nb_fput(ret=ftpnbfput(ftp, file,file, file,fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {// Do whatever you want
echo ".";// Continue upload... ret=ftpnbcontinue(ret = ftp_nb_continue(ret=ftpnbcontinue(ftp);
}
if ($ret != FTP_FINISHED) {
echo "There was an error uploading the file...";
exit(1);
}fclose($fp);
?>`

See Also

Found A Problem?

jascha at bluestatedigital dot com

20 years ago

`There is an easy way to check progress while uploading a file. Just use the ftell function to watch the position in the file handle. ftp_nb_fput will increment the position as the file is transferred.

Example:

fh=fopen(fh = fopen (fh=fopen(file_name, "r"); ret=ftpnbfput(ret = ftp_nb_fput (ret=ftpnbfput(ftp, filename,file_name, filename,fh, FTP_BINARY); while ($ret == FTP_MOREDATA) { print ftell ($fh)."\n"; ret=ftpnbcontinue(ret = ftp_nb_continue(ret=ftpnbcontinue(ftp); } if ($ret != FTP_FINISHED) { print ("error uploading\n"); exit(1); } fclose($fh); ?>

This will print out the number of bytes transferred thus far, every time the loop runs. Coverting this into a percentage is simply a matter of dividing the number of bytes transferred by the total size of the file.

`

marcopardo at gmx dot de

5 years ago

FTP_FAILED = 0 FTP_FINISHED = 1 FTP_MOREDATA = 2