您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

通过PHP脚本从FTP服务器将文件下载到具有Content-Length标头的浏览器,而无需将文件存储在Web服务器上

通过PHP脚本从FTP服务器将文件下载到具有Content-Length标头的浏览器,而无需将文件存储在Web服务器上

只需删除输出缓冲(ob_start()和其他)。

仅使用此:

ftp_get($conn_id, "PHP://output", $file, FTP_BINARY);

虽然如果要添加Content-Length标题,则必须先使用来查询文件大小ftp_size

$conn_id = ftp_connect("ftp.example.com");
ftp_login($conn_id, "username", "password");
ftp_pasv($conn_id, true);

$file_path = "remote/path/file.zip";
$size = ftp_size($conn_id, $file_path);

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($file_path));
header("Content-Length: $size");

ftp_get($conn_id, "PHP://output", $file_path, FTP_BINARY);

添加错误处理)

php 2022/1/1 18:18:00 有486人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶