使用 Readfile 从 FTP 下载文件
Download file from FTP using Readfile
我想让我的用户可以选择从他们在远程 FTP 服务器上的唯一目录下载文件,但是其中一些文件的大小非常大。我试过使用 FileSaver.js but this struggles with large files as I have to read them from the remote server to the local web-server first, then save them to the client. The alternative StreamSaver.js 是一个更好的选择,因为它支持分块,但它几乎没有浏览器支持。
使用 PHP 中的 readfile 效果很好,它将下载流式传输到客户端,同时将数据读入内存。问题是 URL 它从中提取包含 FTP 凭据,因此我不想 link 直接到它。
是否有任何选项可以让我为他们提供某种类型的 "Download" 按钮并仍然使用以下代码?
$file_url = 'ftp://username:password@124.23.148.103/124.23.148.103 port 25665/server.jar';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
这意味着您想要 PHP 从 FTP 读取文件并将其流式传输到您的客户端。您可以使用 PHP FTP
函数,也可以将文件作为输出缓冲区读取,而不是先将其保存到 PHP 服务器。
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
$conn_id = ftp_connect(124.23.148.103);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download file and stream it without save it on php server
if (!ftp_get($conn_id, "php://output", $server_file, FTP_BINARY)) {
echo "There was a problem when reading file\n";
}
ftp_close($conn_id);
您可以在客户端使用 html <form>
元素
<form action="download.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
File name: <input type="text" name="filename"><br>
<input type="submit" value="Download">
</form>
$file_url = "ftp://"
. $_GET["username"]
. ":"
. $_GET["password"]
. "@124.23.148.103/124.23.148.103 port 25665/"
. $_GET["filename"];
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
我想让我的用户可以选择从他们在远程 FTP 服务器上的唯一目录下载文件,但是其中一些文件的大小非常大。我试过使用 FileSaver.js but this struggles with large files as I have to read them from the remote server to the local web-server first, then save them to the client. The alternative StreamSaver.js 是一个更好的选择,因为它支持分块,但它几乎没有浏览器支持。
使用 PHP 中的 readfile 效果很好,它将下载流式传输到客户端,同时将数据读入内存。问题是 URL 它从中提取包含 FTP 凭据,因此我不想 link 直接到它。
是否有任何选项可以让我为他们提供某种类型的 "Download" 按钮并仍然使用以下代码?
$file_url = 'ftp://username:password@124.23.148.103/124.23.148.103 port 25665/server.jar';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
这意味着您想要 PHP 从 FTP 读取文件并将其流式传输到您的客户端。您可以使用 PHP FTP
函数,也可以将文件作为输出缓冲区读取,而不是先将其保存到 PHP 服务器。
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
$conn_id = ftp_connect(124.23.148.103);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download file and stream it without save it on php server
if (!ftp_get($conn_id, "php://output", $server_file, FTP_BINARY)) {
echo "There was a problem when reading file\n";
}
ftp_close($conn_id);
您可以在客户端使用 html <form>
元素
<form action="download.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
File name: <input type="text" name="filename"><br>
<input type="submit" value="Download">
</form>
$file_url = "ftp://"
. $_GET["username"]
. ":"
. $_GET["password"]
. "@124.23.148.103/124.23.148.103 port 25665/"
. $_GET["filename"];
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);