PHP 中的 ftp_fget() 函数
phpprogrammingserver side programming更新于 2025/5/28 15:07:17
ftp_fget() 函数用于从 FTP 服务器下载文件并将其保存到打开的本地文件中
语法
ftp_fget(con,open_file,server_file,mode,startpos);
参数
con − FTP 连接
open_file − 存储数据的文件
server_file − 要下载的服务器文件
mode − 传输模式
startpos −开始下载的位置。在 PHP 4.3.0 中添加。
返回
ftp_fget() 函数在成功时返回 TRUE,在失败时返回 FALSE。
示例
以下是我们将下载服务器文件"demo.txt"并将其保存到打开的本地文件"new.txt"的示例 −
<?php $ftp_server="192.168.0.4"; $ftp_user="amit"; $ftp_pass="tywg61gh"; $con = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login = ftp_login($con, $ftp_user, $ftp_pass); $my_serverfile = "demo.txt"; $local_file = "new.txt"; $file_pointer = fopen($local_file,"w"); if (ftp_fget($con, $file_pointer, $my_serverfile, FTP_ASCII, 0)) { echo "Written to $local_file!"; } else { echo "Error in downloading the $my_serverfile!"; } ftp_close($con); fclose($file_pointer); ?>