FTPサーバーからファイルをダウンロードし、オープンしているローカルファイルに保存します(非ブロッキング):
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$server_file = "somefile.txt";
// open local file to write to
$local_file = "local.txt";
$fp = fopen($local_file,"w");
// initiate download
$d = ftp_nb_fget($ftp_conn, $fp, $server_file, FTP_BINARY)
while ($d == FTP_MOREDATA)
{
// do whatever you want
// continue downloading
$d = ftp_nb_continue($ftp_conn);
}
if ($d != FTP_FINISHED)
{
echo "Error downloading $server_file";
exit(1);
}
// close connection and file handler
ftp_close($ftp_conn);
fclose($fp);
?>
ftp_nb_fget() 関数は、FTPサーバからファイルを取得(ダウンロード)し、オープンしているローカルファイルに保存します(非ブロッキング)。
チップ: この関数は(ftp_fget()とは逆に)ファイルを非同期に取得するので、 ファイルのダウンロード中に他の操作を実行できます。
ftp_nb_fget(ftp_connection,open_file,server_file,mode,startpos);
パラメータ | 説明 |
---|---|
ftp_connection | 必須。 使用するFTP接続を指定する |
open_file | 必須。データを格納するオープンされているローカルファイルを指定する |
server_file | 必須。ダウンロードするサーバファイルを指定する |
mode | 必須。転送モードを指定する。 可能な値:FTP_ASCII または FTP_BINARY |
startpos | 任意。ダウンロードを開始するリモートファイルの位置を指定する |
返り値: | この関数は、次のいずれかの値を返します:
|
---|---|
PHP バージョン: | 4.3+ |