dongyan1548 2013-09-10 20:30
浏览 241
已采纳

链接到HTML中的PDF,该文件没有扩展名,但我知道如何正确打开它是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?

Any thoughts guys/gals?

  • 写回答

2条回答 默认 最新

  • doure8758 2013-09-10 20:34
    关注

    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;
    

    See PHP: stream remote pdf to client browser and Proper MIME media type for PDF files for reference as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用