How to force file download with PHP

How to force file download with PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to force file download with PHP

To ensure a user downloads a file rather than viewing it in a browser or triggering an unwanted redirect, you need to manipulate HTTP response headers before sending any output. The core mechanism relies on setting the Content-Type and Content-Disposition headers correctly within your PHP script.

The most critical header is Content-Disposition. By setting this to attachment, you instruct the browser to treat the payload as a downloadable file rather than inline content. You must also specify a filename using the filename parameter. If you omit the filename, modern browsers might attempt to guess it based on the URL or extension, which can lead to unexpected behavior where the user sees a generic name or is prompted with a confusing dialog box.

Here is a minimal example of how to implement this logic:

<?php
$url = "http://example.com/go.exe";
$filename = "go.exe";

// Read file contents from remote source
$content = file_get_contents($url);

if ($content === false) {
    die("Failed to download file");
}

// Set headers for download
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($content));
header('Content-Disposition: attachment; filename="' . $filename . '"');

// Output the raw content
echo $content;

In this scenario, file_get_contents retrieves the binary data from the remote URL. You must output this data using echo immediately after setting the headers. Crucially, you cannot send any whitespace, BOMs, or other output before calling header(). If you attempt to echo a line break or HTML tag first, PHP will mark headers as already sent, causing a fatal error that prevents the download from working.

A common pitfall in your approach involves mixing redirects with direct file serving. You mentioned using header('location', ...) to force a download. This is fundamentally incorrect for this use case. The Location header tells the browser to navigate to a new URL immediately, terminating the current request. When you set Content-Disposition: attachment alongside a Location header, the browser prioritizes the redirect and ignores the intention to download the file currently being generated or read.

Instead of redirecting, you should serve the binary data directly from your script. This gives you full control over the MIME type and the filename presented to the user. If the file is large, consider using readfile() instead of loading the entire content into memory with file_get_contents(). readfile reads the file stream directly and outputs it, which is more efficient for files exceeding a few megabytes.

<?php
$url = "http://example.com/go.exe";
$filename = "go.exe";

// Stream download for better memory efficiency
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Description: File Transfer');
header("Content-Transfer-Encoding: binary");

readfile($url);
exit;

Note the inclusion of exit after readfile. Since you are forcing a download, you do not want to render any additional HTML or PHP logic afterward. Leaving the script running might inadvertently send more output or trigger side effects that corrupt the file stream.

When dealing with user-uploaded files or dynamically generated content, always sanitize the filename to prevent directory traversal attacks. Never trust the filename parameter sent in a POST request; instead, use it as a hint and map it to a safe path on your server before serving it. If you are working within a Laravel application, you can leverage the Response facade to handle these headers cleanly without writing low-level HTTP logic manually.

Our network