Home >Backend Development >PHP Tutorial >How to Directly Download Files from FTP to User\'s Browser in PHP without Disk Storage?
Downloading Files via PHP Script from FTP to Browser without Disk Storage
Using PHP, files can be efficiently retrieved from FTP servers. However, what if the objective is to directly deliver the file to the user's browser, bypassing local disk storage?
Method Without Output Buffering:
To achieve this, simply remove the output buffering functions (ob_start() and its counterparts):
<code class="php">ftp_get($conn_id, "php://output", $file, FTP_BINARY);</code>
Adding Content-Length Header:
To include the Content-Length header, follow these steps:
Code Example:
<code class="php">$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);</code>
Additional Notes:
The above is the detailed content of How to Directly Download Files from FTP to User's Browser in PHP without Disk Storage?. For more information, please follow other related articles on the PHP Chinese website!