©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
ftp_nb_fput — 将文件存储到 FTP 服务器 (非阻塞)
$ftp_stream
, string $remote_file
, resource $handle
, int $mode
[, int $startpos
= 0
] )ftp_nb_fput() 把已打开的文件内容存储到远程 FTP 服务器
本函数和 ftp_fput() 函数的区别是 本函数是异步上传文件。 所以在文件上传过程中,你的程序还可以执行其他操作。
ftp_stream
FTP 连接标示符。
remote_file
远程文件路径。
handle
已经打开的本地文件指针,当读取到文件末尾时结束。
mode
传输模式。必须是 FTP_ASCII
或
FTP_BINARY
。
startpos
要将文件存储到远程文件的开始位置(即从远程文件的哪个字节位置开始存储)。
返回 FTP_FAILED
或 FTP_FINISHED
或 FTP_MOREDATA
。
Example #1 ftp_nb_fput() 函数例程
<?php
$file = 'index.php' ;
$fp = fopen ( $file , 'r' );
$conn_id = ftp_connect ( $ftp_server );
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );
// 初始化上传
$ret = ftp_nb_fput ( $conn_id , $file , $fp , FTP_BINARY );
while ( $ret == FTP_MOREDATA ) {
// 任何其他需要做的操作
echo "." ;
// 继续上传...
$ret = ftp_nb_continue ( $conn_id );
}
if ( $ret != FTP_FINISHED ) {
echo "There was an error uploading the file..." ;
exit( 1 );
}
fclose ( $fp );
?>
[#1] jascha at bluestatedigital dot com [2004-12-27 18:45:32]
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:
<?php
$fh = fopen ($file_name, "r");
$ret = ftp_nb_fput ($ftp, $file_name, $fh, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
print ftell ($fh)."\n";
$ret = ftp_nb_continue($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.