ftp_fget

(PHP 4, PHP 5, PHP 7, PHP 8)

ftp_fget從 FTP 服務(wù)器上下載一個文件并保存到本地一個已經(jīng)打開的文件中

說明

ftp_fget(
    resource $ftp_stream,
    resource $handle,
    string $remote_file,
    int $mode,
    int $resumepos = 0
): bool

ftp_fget() 函數(shù)用來下載由 remote_file 指定的文件,并寫入到本地已經(jīng)被打開的一個文件中。

參數(shù)

ftp_stream

FTP 連接的鏈接標(biāo)識符。

handle

本地已經(jīng)打開的文件的句柄。

remote_file

遠(yuǎn)程文件的路徑。

mode

傳送模式參數(shù), 必須是 (文本模式) FTP_ASCII 或 (二進(jìn)制模式) FTP_BINARY 中的一個。

resumepos

遠(yuǎn)程文件開始下載的位置。

返回值

成功時返回 true, 或者在失敗時返回 false。

范例

示例 #1 ftp_fget() 例子

<?php

// path to remote file
$remote_file 'somefile.txt';
$local_file 'localfile.txt';

// open some file to write to
$handle fopen($local_file'w');

// set up basic connection
$conn_id ftp_connect($ftp_server);

// login with username and password
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// try to download $remote_file and save it to $handle
if (ftp_fget($conn_id$handle$remote_fileFTP_ASCII0)) {
 echo 
"successfully written to $local_file\n";
} else {
 echo 
"There was a problem while downloading $remote_file to $local_file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>

更新日志

版本 說明
4.3.0 增加了resumepos 的支持。

參見

  • ftp_get() - 從 FTP 服務(wù)器上下載一個文件
  • ftp_nb_get() - 從 FTP 服務(wù)器上獲取文件并寫入本地文件(non-blocking)
  • ftp_nb_fget() - 從 FTP 服務(wù)器獲取文件并寫入到一個打開的文件(非阻塞)