"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Stream FTP Files Directly to the Browser Without Saving Locally?

How to Stream FTP Files Directly to the Browser Without Saving Locally?

Published on 2024-11-10
Browse:499

How to Stream FTP Files Directly to the Browser Without Saving Locally?

Download File from FTP Server to Browser without Saving Locally

This question seeks an efficient way to retrieve a file from an FTP server and send it directly to the user's browser, bypassing local storage and redirects.

The provided PHP function, getFtpFileContents, fetches the file into memory but requires subsequent manual steps to send it to the browser. To remove the need for intermediate storage, simply remove the output buffering code:

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

If you wish to include the Content-Length header, it is necessary to query the file size first:

$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);

Remember to incorporate error handling into your code for robust operation.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3