php - 客户端缺少 Content-Disposition header

标签 php http wireshark

我有一个 php add,它调用 LaTeX,然后将 PDF 传递给浏览器。由于我的用户将为此服务付费,我想确保他们可以选择保存 PDF,而不是一次又一次地访问我的服务器。

exec("cd tex && latex {$_SESSION['sen_id']}.tex && pdflatex {$_SESSION['sen_id']}.tex", $output);
$pdf = substr($file,0,-3).'pdf';
if (file_exists($pdf)) {
  //header('Content-Description: File Transfer');
  header('Content-Type: application/pdf');
  //header('Content-Length: ' . filesize($pdf));
  header('Content-Disposition: attachment;filename='.date('Ymd-His').'-'.basename($pdf));
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: no-cache');
  header('Pragma: no-cache');
  ob_clean();
  flush();
  readfile($pdf);
  exit;
} else {
  echo '<h1>No PDF Produced</h1>';
  echo nl2br(print_r($output,true));
}

使用 Wireshark,我注意到 Content-Disposition header 未设置或未到达客户端。

HTTP/1.1 200 OK\r\n
Date: Tue, 22 Jun 2010 14:15:10 GMT\r\n
Server: Apache/2.0.55 (Ubuntu) mod_jk/1.2.14 mod_python/3.1.4 Python/2.4.3 PHP/5.1.2 mod_ssl/2.0.55 OpenSSL/0.9.8a mod_perl/2.0.2 Perl/v5.8.7\r\n
X-Powered-By: PHP/5.1.2\r\n
Set-Cookie: SESS0d6c65b0599f5b70f6bbc50cfc5b2f94=2b23ba1f74f5f1f641365e9fbb45870d; expires=Thu, 15 Jul 2010 17:48:30 GMT; path=/; domain=.<domain removed>\r\n
Content-Transfer-Encoding: binary\r\n
Expires: 0\r\n
Cache-Control: no-cache\r\n
Pragma: no-cache\r\n
Connection: close\r\n
Transfer-Encoding: chunked\r\n
Content-Type: application/pdf\r\n
\r\n

到目前为止,我发现的提示是“使用 octet-steam”、“不要使用 octet-stream”、“在冒号后放置空格”、“将每个单词大写”和“将文件名用引号引起来” ”。我想有很多幸运的人发布了很多错误信息。

最佳答案

我不知道为什么要删除 header ,但是 this page描述了如何在内容处置 header 中对文件名进行编码及其对浏览器互操作性的影响。

在这些情况下,我选择了我在 PHP 手册页 here 中编写的解决方案(参见第一个示例)。

your case ,您使用的 header 无效,但是,它应该适用于所有主要浏览器。

关于php - 客户端缺少 Content-Disposition header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3094014/

相关文章:

PHP array_diff 怪异

java - 在 PHP 网络服务和 Android/Java 应用程序之间保护数据的最佳方法是什么

html - 如果外部包含的文件(例如 JavaScript 或 CSS)在页面上包含两次(或更多次),是否会有两次(或更多次)网络请求?

swift - 在 Alamofire 4 中调试 HTTP POST 的正确方法; HTTP POST 不工作

wireshark - 如何远程对 pcap 文件运行 tail?

ssl - 结合 Wireshark 使用 Burp Suite 代理嗅探 https/SSL 流量

php - 将嵌套 sql SELECT 语句中的 SELECT 结果分配给变量 - PHP

android - Retrofit返回错误307,在android中调用web服务

c - Wireshark 未打开 pcap_dump 文件

php - 使用 PHP 连接数据库的最安全方法有哪些?