链接到html中的PDF,该文件没有扩展名,但我知道这是pdf如何使其正确打开 [英] Link to a PDF in html, the file has no extension, but I know it is pdf how to make it open appropriately
问题描述
第一篇文章。我正在为一个客户端的项目,他们有pdf文件上传到文件结构(LAMP堆栈),但文件没有扩展名。假设这些文件必须是PDF,我将如何让浏览器了解,并相应地打开它们?显然,通过添加文件扩展名,这将突然工作,但我不能改变他们的系统工作方式,这将导致太多的变化,他们在一个紧迫的截止日期。至于保存一个临时副本在某个地方,我可以这样做,但我希望一个更好的解决方案。有没有办法向浏览器建议他们以某种方式打开文件?
First post. I'm working on a project for a client where they have pdf files uploaded to a file structure (LAMP Stack) but the files have no extensions on them. Under the assumption that those files have to be PDF how would I get the browsers to understand that, and open them accordingly? Obviously with adding the file extensions this would suddenly work but I can't change the way their system works, it would result in too many changes and they are on a tight deadline. As for saving a temporary copy somewhere, I could do that, but I was hoping for a better solution. Is there a way to suggest to the browsers that they open a file a certain way?
任何想法家伙/ gals?
Any thoughts guys/gals?
推荐答案
您只需在标题中设置应用程序类型和文件名,如下所示:
You just set the application type and file name in the headers, like so:
// 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媒体类型,以供参考。
这篇关于链接到html中的PDF,该文件没有扩展名,但我知道这是pdf如何使其正确打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!