Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As the others said, headers are sent at the first output if nothing else is specified, so your echo will trigger the script to send the standard headers for the content type, as specified by the web server - probably html or something like it.</p> <p>Furthermore you have other unnecessary stuff in you code, so I'd suggest something like this:</p> <pre><code>if(is_file($fullPath)) { $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts['extension']); switch($ext) { case 'pdf': header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $name . '"'); break; default: header('Content-type: application/octet-stream'); header('Content-length: ' . filesize($fullPath)); header('Cache-control: private'); //use this to open files directly } readfile($fullPath); } exit; </code></pre> <p>The biggest differences is, that the initial check just checks if the file exists and doesn't open the file. The output is done by just opening the file and writing it directly to the output buffer. I removed the partial <code>Content-Disposition</code> header from the default case, as it will by default do the same or better, and moved Content-lenght and Cache-control to the default case, as they're not needed for the pdf download. Also, I changed it to serve the file with filename <code>$name</code>. I suspect it should be the other way around if <code>$name</code> is defined outside the given scope.</p> <p>If the script fails, you may just have the wrong path to the pdf-file. Try putting this code before the one above</p> <pre><code>echo 'The file "' . $fullPath . '" ' . (is_file($fullPath) ? 'exists' : 'doesn\'t exist'); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload