gpt4 book ai didi

php - 如何使用 PHP 强制文件下载

转载 作者:IT老高 更新时间:2023-10-28 11:49:37 25 4
gpt4 key购买 nike

我想要求在用户使用 PHP 访问网页时下载文件。我认为它与 file_get_contents 有关,但不确定如何执行。

$url = "http://example.com/go.exe";

使用 header(location) 下载文件后,它不会重定向到另一个页面。它只是停止。

最佳答案

阅读有关内置 PHP 函数的文档 readfile

$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);

还要确保根据您的文件 application/zip、application/pdf 等添加正确的内容类型 - 但前提是您不想触发另存为对话框。

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

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