3

第一篇文章。我正在为客户开发一个项目,他们将 pdf 文件上传到文件结构(LAMP 堆栈),但文件上没有扩展名。在这些文件必须是 PDF 的假设下,我如何让浏览器理解并相应地打开它们?显然,通过添加文件扩展名,这会突然起作用,但我无法改变他们系统的工作方式,这会导致太多的变化,而且他们的截止日期很紧。至于在某处保存临时副本,我可以这样做,但我希望有更好的解决方案。有没有办法向浏览器建议他们以某种方式打开文件?

有什么想法吗?

4

2 回答 2

0

您只需在标题中设置应用程序类型和文件名,如下所示:

// This points to the file in question, note that it doesn't 
// care whether it has an extension on the name or not.
$filePathOnDisk = '/path/to/your/pdffile';

// You can make this whatever you like, it doesn't have to 
// be the same as the file name on the disk! This is the name of the file your end 
// user will see when they are asked if they want to save. open, etc in the browser.
$fileName = 'file.pdf'; 

$data = file_get_contents($filePathOnDisk); 
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=$fileName");
echo $data;

请参阅PHP:将远程 pdf 流式传输到客户端浏览器PDF 文件的正确 MIME 媒体类型以供参考。

于 2013-09-10T20:34:06.077 回答
0

经过测试

您可以使用以下提示用户将 (PDF) 文件保存在其计算机上的方法。

注意不同的文件名。

一个是将上传/提示给用户download_example.pdf的文件,而另一个是没有扩展名的文件,如readfile('example');

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download_example.pdf"');
readfile('example');
?>
于 2013-09-10T21:17:41.300 回答