Using php to force download a pdf

Using php to force download a pdf

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Using PHP to Force Download a PDF: A Debugging Guide

When building a web interface that requires users to download specific documents, the transition from HTML navigation to server-side file handling often trips up developers. You might have a perfectly functional button in your view, but once you hit the browser's "Save As" dialog, the resulting file is corrupted or unreadable. This scenario frequently stems not from complex encryption issues, but from fundamental misalignments between your file paths and your HTTP headers.

Consider a setup where an HTML button points to a PHP script intended to serve a PDF:

<a href="/scripts/download.php">
    <img src="/images/download.gif" alt="Submit button" />
</a>

The associated PHP script attempts to force the browser to treat the output as a downloadable attachment using Content-Disposition and readfile:

<?php
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=documents/ECM_IT_ResumeDownload.pdf');
readfile('/home/user/project/html/documents/ECM_IT_ResumeDownload.pdf'); // Assuming a specific path structure
?>

If you encounter an error message stating that Adobe Reader cannot open the file because it is "damaged" or not a supported type, your first instinct might be to suspect the PDF generation logic. However, in many cases, the root cause is a simple filesystem discrepancy revealed by a PHP warning.

In the specific case provided, the user reports a warning: readfile(documents/ECM_IT_ResumeDownload.pdf) [function.readfile]: failed to open stream: No such file or directory. Despite the file existing at html/documents/ECM_IT_ResumeDownload.pdf, the script fails because the path passed to readfile does not match the physical location of the resource.

PHP scripts run on the server, and they do not automatically guess relative paths based on the current URL structure unless explicitly told to. When you call header('Content-Disposition: attachment; filename=...'), you are telling the browser what to name the file once it is saved locally. This header does not affect where PHP looks for the source file. If your script runs from /scripts/ but tries to read documents/file.pdf, PHP will look in /scripts/documents/file.pdf. If that directory doesn't exist relative to the web root, or if the path includes a virtual directory prefix that isn't actually present on the disk, readfile throws a fatal error.

To resolve this, you must ensure the path passed to readfile() is an absolute filesystem path or a correct relative path from the document root. A robust approach involves using realpath() to normalize the path and prevent "No such file or directory" errors:

<?php
$file_path = __DIR__ . '/html/documents/ECM_IT_ResumeDownload.pdf'; // Adjust based on actual structure
if (file_exists($file_path)) {
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename=ECM_IT_ResumeDownload.pdf');
    readfile($file_path);
} else {
    http_response_code(404);
    echo 'File not found';
}
?>

There is also a subtle but critical distinction regarding the filename attribute in the Content-Disposition header. While browsers generally respect this, some older clients or specific security policies might ignore it if it contains certain characters or if the path includes directory separators that look suspicious to the client's sandbox. It is best practice to provide just the filename without the leading directory structure in the header, even if the file lives in a subfolder on the server.

If you are using a framework like Laravel, managing file downloads becomes more streamlined through the Response facade or controller helpers, which handle these headers and path resolution automatically. However, understanding the underlying mechanics remains essential for debugging when things go wrong. The "damaged file" error is almost exclusively a symptom of the server failing to read the bytes correctly due to a path mismatch, rather than an issue with the PDF format itself.

Always verify that readfile executes without warnings by enabling PHP error reporting in your development environment. Once the path issue is resolved and the stream is successfully opened, the binary data will be transmitted exactly as stored on disk, resulting in a valid, openable PDF.

Our network