Forcing File Downloads in PHP with Remote File Storage
To add a "Download this File" feature to a website while preventing direct playback in the browser, consider the following PHP solution:
// Locate the remote file. $file_name = 'file.avi'; $file_url = 'http://www.myremoteserver.com/' . $file_name; // Configure the download settings. header('Content-Type: application/octet-stream'); header('Content-Transfer-Encoding: Binary'); header('Content-disposition: attachment; filename="' . $file_name . '"'); // Download the remote file content. readfile($file_url); // Ensure no output follows the download. exit;
First, the PHP script locates the remote file and sets up the necessary HTTP headers to force a download instead of playback.
The script uses the readfile() function to retrieve the remote file's content. Note that the fopen_wrappers setting must be enabled for PHP to read remote URLs.
Finally, the script exits to prevent any further output that could interfere with the download process.
以上是如何在 PHP 中強制從遠端儲存下載檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!