Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server
Just remove the output buffering (ob_start()
and the others).
Use just this:
ftp_get($conn_id, "php://output", $file, FTP_BINARY);
Though if you want to add Content-Length
header, you have to query file size first using 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);
(add error handling)
For more broad background, see:
List and download clicked file from FTP