gpt4 book ai didi

php - 在网络服务器上通过PHP下载文件问题

转载 作者:行者123 更新时间:2023-11-29 15:15:06 25 4
gpt4 key购买 nike

有人可以帮我解决一些 PHP 问题吗?

这是我的代码:

$mr = mysqli_fetch_assoc($ms);
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($mr['flink']) . "\"");
readfile($mr['flink']);
header("Location: ref.php?action=viewdeti&id=".$_GET['id']);

其中 $mysqli 是我的 mysql 连接,$id 是 $_GET['id']。

$mr['flink'] 是指向我的服务器上托管的文件的链接。

现在的问题是:

当代码托管在我的服务器上时,代码运行成功并允许从本地主机下载文件并将我重定向到正确的页面。

当代码托管在 Web 服务器托管上并且运行脚本时,不会下载托管在 Web 服务器上的文件,而是在浏览器中将文件作为代码打开,并且不会重定向我。

我会附上照片来看看实际问题。

服务器上的 apache、mysql、php 版本与本地主机上的版本相同。

下载的文件为*.pdf

如果您有任何想法,请帮助我。

提前致谢。

最诚挚的问候,罗森

WEB SERVER LOCALHOST

最佳答案

$mr = mysqli_fetch_assoc($ms);

header('Content-Transfer-Encoding: binary'); // mainly for Gecko browsers
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($mr['flink'])) . ' GMT'); // modified date of file, useful with caching
header('Accept-Ranges: bytes'); // To resume download
header('Content-Length: ' . filesize($mr['flink'])); // file size
header('Content-Encoding: none'); // don't worry about encoding
header('Content-Type: application/pdf'); // mime type if the file is PDF, change if required
header('Content-Disposition: attachment; filename=' . basename($mr['flink'])); // force browser to show the Save As dialog
readfile($mr['flink']); // actually download the file, required
header("Location: ref.php?action=viewdeti&id=".$_GET['id']); // copied from question

每一行都有注释并进行了解释。

除上述内容外,请确保实际文件具有 .pdf 扩展名。不知何故,该文件似乎缺少远程服务器上的扩展名。

关于php - 在网络服务器上通过PHP下载文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59739706/

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