使用 Readfile() 将 FTP 文件下载到客户端

Download FTP file to client w/ Readfile()

我正在为我们的客户构建一个基于 Web 的文件管理界面,我试图在其中促进从远程 (FTP) 服务器到客户端的下载,而无需先下载到本地 Web 服务器。

我发现 Readfile() 完全满足需要,非常适合基于 Web 的下载以及来自 public FTP 服务器的下载。问题是当通过 FTP url 指定凭据时,它显然不再有效。我在网上找到了其他有关此问题的报告,但到目前为止还没有解决方案或解决方法。

$file_url = 'ftp://username:password@198.2.148.130/198.2.148.130%20port%2025665/server.properties';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url);

是否有任何变通方法可以使其按预期运行?我对如何解决这个问题感到困惑,它看起来更像是一个错误而不是限制。

URL 中的 URL 编码空格 (%20) 似乎是罪魁祸首。

只使用文字空格:

$file_url = 'ftp://username:password@198.2.148.130/198.2.148.130 port 25665/server.properties';

在 PHP FTP URL 包装器中以这种方式指定凭据是完全有效的。