Current Location: Home> Latest Articles> How to force the browser to download files instead of opening them through header()

How to force the browser to download files instead of opening them through header()

M66 2025-05-18

In web development, sometimes we want users to be able to download files instead of viewing them in a browser. At this time, we can use PHP's header() function to achieve this requirement. The header() function allows you to send raw HTTP header information and can control the behavior of the browser.

This article will show you how to force the browser to download a file through the header() function instead of opening it directly.

Step 1: Prepare the file

First, you need to make sure the file is ready and you know its path. For example, suppose you have a file located in a directory on the server with the path /path/to/your/file.txt . You want the user to download the file instead of viewing it directly in the browser.

Step 2: Use the header() function to set the correct header

In order to force the browser to download the file, the correct HTTP header must be set. PHP's header() function allows you to send various HTTP header information, the specific steps are as follows:

 <?php
// Set file path
$file = '/path/to/your/file.txt';

// Check if the file exists
if (file_exists($file)) {
    // Set the correct one MIME type
    header('Content-Type: application/octet-stream');
    
    // Specify the downloaded file name
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    
    // Set file size
    header('Content-Length: ' . filesize($file));
    
    // Clear the output buffer
    ob_clean();
    flush();
    
    // Output file content to browser
    readfile($file);
    exit;
} else {
    // If the file does not exist,Prompt error
    echo 'file not found!';
}
?>

Code explanation:

  • header('Content-Type: application/octet-stream')
    This line of code tells the browser that the file is a binary stream and the browser should process it as a file instead of trying to display it directly.

  • header('Content-Disposition: attachment; filename="file.txt"')
    Here, the file download method is set as attachment ( attachment ), and the file name at download is specified as file.txt . You can modify the file name as needed.

  • header('Content-Length: ' . filesize($file))
    This line of code sets the size of the file to help the browser understand the size of the file and facilitates the display of download progress.

  • ob_clean() and flush()
    These functions are used to clear the output buffer of PHP to ensure that the file content is correctly transferred to the browser.

  • readfile($file)
    This function outputs the file contents to the browser, triggering the file download.

  • exit
    Calling exit() to terminate the execution of the PHP script to ensure that there is no other unnecessary output during the file download process.

Step 3: Test

Now you can access PHP scripts in your browser. Assuming the script file name is download.php , you just need to visit http://m66.net/download.php (note that you replace the URL with your actual file path). If everything is configured correctly, the browser will prompt you to download the file instead of opening it directly in the browser.

Frequently Asked Questions

  1. File cannot be downloaded <br> If the file is not downloaded, make sure the file path is correct and that the PHP script has sufficient permissions to access the file.

  2. File is empty after downloading <br> Make sure that the file contents are not corrupted and that the output buffer is not cleaned incorrectly. You can try commenting out ob_clean() and flush() to see if it works.

  3. MIME type issue for downloading files <br> If the file type is special (such as PDF, ZIP, etc.), you can set different MIME types according to the file type. For example:

     header('Content-Type: application/pdf'); // for PDF document
    header('Content-Type: application/zip'); // for ZIP document
    

In this way, PHP's header() function helps you control the download behavior of files, allowing users to download files as expected, rather than viewing them directly in the browser.