首頁 > 後端開發 > php教程 > 如何在 PHP 中強制從遠端儲存下載檔案?

如何在 PHP 中強制從遠端儲存下載檔案?

Susan Sarandon
發布: 2024-11-15 01:46:02
原創
649 人瀏覽過

How to Force File Downloads in PHP from Remote Storage?

透過遠端檔案儲存強制在PHP 下載檔案

在網站上新增「下載此檔案」功能,同時防止直接播放在瀏覽器中,請考慮下列PHP 解:

// 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;
登入後複製

首先,PHP腳本找到遠端檔案並設定設定必要的 HTTP 標頭以強制下載而不是播放。

腳本使用 readfile() 函數擷取遠端檔案的內容。請注意,必須啟用 fopen_wrappers 設置,PHP 才能讀取遠端 URL。

最後,腳本退出以防止任何可能幹擾下載過程的進一步輸出。

以上是如何在 PHP 中強制從遠端儲存下載檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板