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

标签 php mysql file pdf download

有人可以帮我解决一些 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/

相关文章:

javascript - Laravel 5 AJAX 重定向问题获取 'Class App\Http\Controllers\does not exist'

mysql - 如果在同一个表上被插入轰炸,你会期望 MySql 丢弃记录吗?

php - 使用 Yii 框架按年份进行比较

php - 修改MP3文件

golang 文件上传失败

c - 当给定无效路径时,freopen()创建一个文件

php - 在文件中间覆盖

php - 循环修改数组

php - 为什么这个 javascript 不能按照我们在弹出窗口中的目的运行

php - 将数据从 PC 应用程序传输到远程托管数据库