Download files from server php

Download files from server php

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

Stefan Bogdanescu

Founder & Senior Architect

Download files from server php

Listing directory contents and offering a download button is a fundamental requirement for any file management interface built in PHP. The mechanics are straightforward, but getting it right requires handling headers correctly and ensuring the browser behaves as expected.

To initiate a file download, you must instruct the browser to treat the response body as an attachment rather than displaying it inline. This involves sending two critical HTTP headers before outputting any content. First, Content-Type dictates the MIME type of the file; for example, application/pdf for documents or image/jpeg for photos. If this header is missing or incorrect, the browser might try to render the binary data as text, resulting in a garbled display. Second, and often more importantly, you must set the Content-Disposition header with the value attachment. This tells the user agent to prompt for a save dialog immediately upon clicking the link.

The code snippet below demonstrates the standard approach using raw output headers:

<?php
$file = 'projects/my_project.mdb'; // Path relative to web root or absolute

// Determine MIME type based on extension
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->file($file);

// Force download header
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Cache-Control: no-cache, must-revalidate'); // Prevent browser caching issues
header('Pragma: no-cache');
header('Expires: 0');

// Output the file content
readfile($file);
exit;

In this implementation, readfile() is preferred over manually reading with fopen() and echo because it handles the buffering automatically. Attempting to echo large files line-by-line can cause memory issues or timeouts if the buffer isn't managed correctly by the server. Additionally, including Cache-Control and Pragma headers is a best practice to prevent the browser from storing a stale version of the file after an update, which is a common source of support tickets.

When dealing with mixed file types like MDB, JPG, and PDF, dynamically detecting the MIME type using PHP's finfo extension adds robustness. Hardcoding the MIME type for every possible extension is error-prone, especially if you need to support less common formats. However, be aware that some server configurations or strict Content Security Policies (CSP) might block downloads based on these headers. If you encounter issues where the download fails silently, check your Allow-Origin headers or ensure your server doesn't strip the Content-Disposition header upon proxying.

Security is a non-negotiable aspect of this operation. Never trust file names provided directly by the user to construct the path. Always validate that the resulting absolute path resolves strictly within your intended upload directory. A simple check involving realpath() and verifying it starts with the canonical path of your target folder prevents directory traversal attacks where an attacker could download sensitive files from the server root.

For larger projects, consider offloading the file serving logic to a background job if you are using a framework like Laravel. While raw PHP is perfectly capable of this task, high-traffic applications might benefit from queueing large file transfers to avoid blocking the web server thread. If you are building an application with Laravel, you can leverage its Storage facade to handle disk operations consistently across different environments.

If you need a dedicated resource for handling secure file storage and delivery patterns within the broader ecosystem of PHP development, resources like https://laravelcompany.com offer insights into modern infrastructure practices, though understanding the underlying HTTP protocol remains essential regardless of the framework used.

Our network