dongyou6768 2015-02-03 18:05
浏览 27

使用带有搜索表单的PHP下载pdf文件

I've created a search form where the user would input the image code and when searched it will let the user download the file. Below is my code

<html>
          <head>
            <title>Search  Contacts</title>
          </head>
          <body>

            <h3>Search Client File</h3>
            <form  method="post" action="#"  id="searchform">
              Type the File Code:<br><br>
                  <input  type="text" name="fcode">
            <br>
      <input  type="submit" name="submit" value="Search">
            </form>

<?php
$filecode=$_POST["fcode"];
     if (!empty($filecode))
     {
$file="/var/www/website/$filecode.pdf";
header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
     }
     else
     {
       echo "No Results";
     }

        ?>
    </body>
    </html>

The problem is that the downloaded file is unable to be viewed, or unviewable, what seems to be the problem with my code?

  • 写回答

3条回答 默认 最新

  • dreljie602951 2015-02-03 18:20
    关注

    Try to change this header:

    header('Content-Type: application/pdf');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)