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");
// download server file and save it to open local file
if (ftp_fget($ftp_conn, $fp, $server_file, FTP_ASCII, 0))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
// close connection and file handler
ftp_close($ftp_conn);
fclose($fp);
?>
ftp_fget() 関数は、FTPサーバからファイルを取得(ダウンロード)し、オープンしているローカルファイルに保存します。
ftp_fget(ftp_connection,open_file,server_file,mode,startpos);
パラメータ | 説明 |
---|---|
ftp_connection | 必須。 使用するFTP接続を指定する |
open_file | 必須。データを格納用のオープンしているローカルファイルを指定する |
server_file | 必須。ダウンロードするサーバファイルを指定する |
mode | 必須。転送モードを指定する。指定可能な値: FTP_ASCII または FTP_BINARY |
startpos | 任意。ダウンロードを開始するリモートファイル内の位置を指定する |
返り値: | 成功した場合はTRUE、失敗した場合はFALSEを返します |
---|---|
PHP バージョン: | 4+ |
PHP 変更歴 | startposのパラメータは、PHP 4.3.0で追加されました |