-1

My website contains a section that allows users to access restricted PDF files if they have access to them. Basically, they arrive at a page which has a nav bar at the top, allowing them to cycle between PDF files. Below this, there is an iframe which is linked to a PHP page that does the retrieval of the file, and displays it. This file is called falr_pdf.php, and depending on how it is accessed, the PDF file is either displayed inline, or downloaded as an attachment.

Here is the navbar page, along with the falr_pdf.php IFRAME embedded, to give you a clear idea.

https://i.imgur.com/VspEBHg.png

As you can see, the PDF should be displayed inline, and then if the user clicks "Download File", the same page is opened in a new tab, but to download the file as an attachment instead of displaying it inline.

My problem is that this works flawlessly for small PDF files, but anything larger than about 1.6MB will take an incredibly long time to display inline. However, if they are downloaded using the direct link instead, they download at normal speed, very quickly. Here is the code I am using...

$path = $falr_filesbasedir . "/" . $folder . "_pdf/" . $file . ".pdf";
$public_name = basename($path);
header('Content-Length:'.filesize($path));
header('Content-type: application/pdf');
header('Content-Disposition: ' . $method . '; filename="' . $public_name . '"');
readfile($path);
exit;

Now I know that the issue is not in any of my variables or links, since it works fine for every type of small file, and the download works fine for large files. It's only that large files don't display inline correctly.

$method is only ever "inline" or "attachment"

Is this a bug with readfile()? Or is it something to do with my PHP settings?

I am at a complete loss here.

j08691
  • 204,283
  • 31
  • 260
  • 272
Aidan
  • 3
  • 1
  • Could be you php.ini settings. See if this helps: https://stackoverflow.com/questions/19509440/increase-the-limit-of-file-download-size-in-document-management-system – bos570 Apr 02 '18 at 20:51
  • Your probably just sending too big a chunk of data to the browser at one time. There are resource limits on the browser, at some point you hit them. There are some tricks with output buffering you can try, but I'm not sure it will help in this case, because you cant display part of a PDF. You can try some of the ini setting above and they may help some, but sooner or later you will hit a limit. – ArtisticPhoenix Apr 02 '18 at 20:57
  • Download is faster because the browser just dumps the output into a temporary or partial file as it receives the packets of data. It doesn't have to remember them and render a PDF on the screen. – ArtisticPhoenix Apr 02 '18 at 21:04
  • I figured out that the problem was not with the PHP, but with the browser I was using to test. On Microsoft Edge, apparently loading a PDF file inline requires two requests, and if both requests are served the PDF data, it doesn't work for large files. – Aidan Apr 03 '18 at 18:06

1 Answers1

0

Have you tried looking at your performance waterfall? Have you tried accessing the file served directly from the webserver as a static document? Have you tried measuring timings in your php code and comparing that with what's happening in the browser? What is the latency and bandwidth between client and server? Whatt happens when you download the file using a client running on the same host as the server? Are there any signs of stress on the server host? These are the basic questions you should start by investigating.

At a guess, the most likely cause is buffering in php or on the webserver. If you unroll the readfile into read/write operations with flushes ever 200kb you can test this. But you need to start measuring properly.

symcbean
  • 47,736
  • 6
  • 59
  • 94