如何用PHP从FTPS服务器下载文件?

问题描述 投票:0回答:1

我尝试使用 ftp_fget 函数从 PHP 中的 FTPS 服务器下载文件,但遇到无法解决的错误。

这是我正在使用的代码:

$ftp_conn = ftp_ssl_connect('ftps.test.com', 2222) or die("Could not connect to");
$login = ftp_login($ftp_conn, 'user', 'pwd');
if ($login) {
    echo "Successfully logged in";
} else {
    echo "Log in failed";
}

$local_file = "test.txt";
$fp = fopen($local_file,"w");

//this is the path on server where file located
$rsr_product_file = 'keydealer/categories.txt';

// download server file and save it to open local file
if (ftp_fget($ftp_conn, $fp, $rsr_product_file, FTP_ASCII, 0)) {
    echo "Successfully written to $local_file.";
} else {
    echo "Error downloading $rsr_product_file.";
}

执行后,脚本成功登录服务器,但尝试下载文件时失败。这是我收到的输出:

成功登录警告:ftp_get():语法错误,命令无法识别在/chroot/home/test/test.y.net/html/pub/Newftp.php第35行下载keydealer/categories.txt时出错

错误消息表明存在语法错误或无法识别的命令,但我无法弄清楚出了什么问题。我怀疑问题出在 ftp_fget 函数中的某个地方,但我不确定如何继续。

有人能指出我解决这个问题的正确方向吗?

php file ftp
1个回答
0
投票

我知道这是一年多前的事了,但这里的解决方法是启用被动模式。

$ftp_conn = ftp_ssl_connect('ftps.test.com', 2222) or die("Could not connect to");
$login = ftp_login($ftp_conn, 'user', 'pwd');
if ($login) {
    echo "Successfully logged in";
} else {
    echo "Log in failed";
}

ftp_pasv($ftp_conn, true);    //ADD THIS LINE

$local_file = "test.txt";
$fp = fopen($local_file,"w");

//this is the path on server where file located
$rsr_product_file = 'keydealer/categories.txt';

// download server file and save it to open local file
if (ftp_fget($ftp_conn, $fp, $rsr_product_file, FTP_ASCII, 0)) {
    echo "Successfully written to $local_file.";
} else {
    echo "Error downloading $rsr_product_file.";
}
© www.soinside.com 2019 - 2025. All rights reserved.