下载FTP文件到客户端w/ Readfile()


Download FTP file to client w/ Readfile()

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

我发现Readfile()做的正是我们所需要的,无论是基于web的下载,还是来自公共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);

是否有任何变通方法可以使此操作按预期进行?我对如何修复这个问题感到困惑,它看起来更像是一个bug,而不是一个限制。

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

只使用文字空格:

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

在PHP FTP URL包装器中这样指定凭据是完全有效的