duancoubeng5909 2014-05-12 15:01
浏览 32
已采纳

POST变量导致阅读PDF的问题

On my website, I allow an admin to read a user's resumes. These resumes are stored outside of the root folder. In order to read them, I submit a form with the user's ID, get the path to their resume from the database and display it to the admin in a new tab as a PDF. Below is the code for displaying the PDF.

$file = "file_name.pdf";
$path = "C:/path/to/file/" . $file;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
header('Accept-Ranges: bytes');
readfile($path);

In order to submit the form to get to this code, I do the following in javascript.

$("#resume-user-id").val(ui.target.attr("id"));
$("#resume-form").submit();

And here is the form code.

<form id="resume-form" method="post" action="/account/profile/preview_resume.php" target="_blank">
    <input id="resume-user-id" type="hidden" name="user_id"></input> 
</form>

Now, this all works just just fine and dandy and the PDF is displayed correctly because I have hard coded in the file name and path. However, as soon as I access a post variable at all, the PDF doesn't load. Even if I keep the file name hard coded and just check if the post variables are set by using the isset function, everything just breaks. The page looks like it is trying to load a PDF, but it just keeps loading, and loading, and loading...

Any ideas what might be causing this?

--UPDATE--

I removed the header('Accept-Ranges: bytes'); line, and now it works fine (mostly). What exactly does this header do, and why would it be causing issues in this situation? I say 'mostly' fine because all but one of my test PDF's work. One of them however, I just get a message that says "Cannot load PDF"... Not sure why this is, or how it is different from the others.

  • 写回答

1条回答 默认 最新

  • duanji1043 2014-05-13 19:51
    关注

    Ok, I finally figured it out. After the update to the original question, I was still getting some errors with PDF files larger than 1MB. Chrome would not display the files and IE(compatibility mode) would corrupt them. I did not test it in firefox before finding a solution. My final script looks like this:

    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $file . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($path));
    header('Accept-Ranges: none');
    ob_clean();
    flush();
    readfile($path);
    

    Note that $file is the name of the file (i.e. filename.pdf) and $path is the path to the file (i.e. C:/path/to/file/filename.pdf). To the original question, I added ob_clean(); and flush(); before the readfile call, and I set the 'Accept-Ranges' header to 'none'.

    PDF file is now viewable in chrome and IE (compatibility mode). In firefox, the file actually downloads, but I am not too worried about that.

    Similar question and answer found here: PHP stream PDF with fread and readfile produce a damaged pdf

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输