gpt4 book ai didi

php - 如何强制下载文件?

转载 作者:IT王子 更新时间:2023-10-29 00:05:46 27 4
gpt4 key购买 nike

我希望在我的一个网站上的每个视频下方添加一个“下载此文件”功能。我需要强制用户下载文件,而不是仅仅链接到它,因为有时会开始在浏览器中播放文件。问题是,视频文件存储在单独的服务器上。

我可以用 PHP 强制下载吗?

最佳答案

你可以试试这样的:

<?php

// Locate.
$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;

// Configure.
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");

// Actual download.
readfile($file_url);

// Finally, just to be sure that remaining script does not output anything.
exit;

我刚刚对其进行了测试,它对我有用。

请注意,要使 readfile 能够读取远程 url,您需要拥有 fopen_wrappers已启用。

关于php - 如何强制下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/531292/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com