CakeFest 2024: The Official CakePHP Conference

ftp_nb_fput

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

ftp_nb_fputオープン中のファイルを FTP サーバーに保存する(非ブロッキング)

説明

ftp_nb_fput(
    FTP\Connection $ftp,
    string $remote_filename,
    resource $stream,
    int $mode = FTP_BINARY,
    int $offset = 0
): int

ftp_nb_fput() は、ファイルポインタが指すデータを FTP サーバー上のリモートファイルへアップロードします。

ftp_fput() との違いは、この関数が 非同期処理でファイルをアップロードするということです。そのため、 ファイルをアップロードしている最中に別の処理を行うことができます。

パラメータ

ftp

FTP\Connection クラスのインスタンス

remote_filename

リモートファイルのパス。

stream

ローカルでオープンされているファイルのポインタ。 ファイルの終端まで進むと読み込みが終了します。

mode

転送モード。FTP_ASCII または FTP_BINARY のどちらかを指定する必要があります。

offset

リモートファイル内での、アップロード開始位置。

戻り値

FTP_FAILEDFTP_FINISHED あるいは FTP_MOREDATA を返します。

変更履歴

バージョン 説明
8.1.0 引数 ftp は、FTP\Connection のインスタンスを期待するようになりました。 これより前のバージョンでは、リソース を期待していました。
7.3.0 mode パラメータはオプションになりました。 これより前のバージョンでは、このパラメータは必須でした。

例1 ftp_nb_fput() の例

<?php

$file
= 'index.php';

$fp = fopen($file, 'r');

$ftp = ftp_connect($ftp_server);

$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);

// アップロードを開始する
$ret = ftp_nb_fput($ftp, $file, $fp, FTP_BINARY);
while (
$ret == FTP_MOREDATA) {

// 何かお好みの動作を
echo ".";

// アップロードを継続する…
$ret = ftp_nb_continue($ftp);
}
if (
$ret != FTP_FINISHED) {
echo
"There was an error uploading the file...";
exit(
1);
}

fclose($fp);
?>

参考

  • ftp_nb_put() - FTP サーバーにファイルを保存する(非ブロッキング)
  • ftp_nb_continue() - ファイルの取得/送信を継続する(非ブロッキング)
  • ftp_put() - FTP サーバーにファイルをアップロードする
  • ftp_fput() - オープン中のファイルを FTP サーバーにアップロードする

add a note

User Contributed Notes 2 notes

up
1
jascha at bluestatedigital dot com
19 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 ($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.
up
-3
marcopardo at gmx dot de
4 years ago
FTP_FAILED = 0
FTP_FINISHED = 1
FTP_MOREDATA = 2
To Top