如何在 PHP 中强制从远程存储下载文件?

Susan Sarandon
发布: 2024-11-15 01:46:02
原创
523 人浏览过

How to Force File Downloads in PHP from Remote Storage?

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中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板