FTP从服务器直接下载文件到客户端

2022-01-09 00:00:00 download ftp php

我尝试将文件从 FTP 服务器下载到客户端.如果我使用ftp_get,文件被下载到PHP服务器,它可以将输出写入浏览器.所以下载过程是

I try to download a file from FTP server into client. If I use ftp_get, the file is downloaded into PHP server, which can write the output into browser. So the download process is

FTP server -> PHP server -> client

这会使流量翻倍 - 这对下载大文件很不利.有一种方法可以将文件直接写入此处描述的浏览器:流式 FTP 下载到输出 - 但无论如何数据都会流经 PHP 服务器,对吗?

This doubles traffic - this is bad in downloading big files. There is a way how to write the file directly into the browser described here: Stream FTP download to output - but the data flows through PHP server anyway, am I right?

有什么方法可以建立这个下载(如果是,怎么做?),还是根本不可能?

Is there any way how to establish this download (if yes, how?), or is it principially impossible?

FTP server -> client

它还应该以安全的方式与非匿名 FTP 服务器一起使用.

it should work also with non-anonymous FTP servers in secure way.

推荐答案

试试下面的代码.

$curl = curl_init();
$file = fopen("ls-lR.gz", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);

谢谢.

相关文章