http header for downloading Microsoft Word and Excel files

http header for downloading Microsoft Word and Excel files

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Dynamic Filenames and Extensions in HTTP Headers

When generating downloadable documents like Microsoft Word or Excel files, the Content-Disposition header is your primary mechanism for controlling how the browser handles the response. The header you provided demonstrates a common scenario where dynamic content meets static expectations: you want to download a file generated with variables, but the resulting filename lacks the correct extension (.docx).

The core issue lies not in the PHP logic used to construct the variable $No_$Name_$Test, but rather in how the browser interprets the MIME type and the Content-Disposition directive. When you set Content-Type: application/msword, older browsers or specific configurations might associate this MIME type strictly with .doc files, ignoring any extension provided in the filename parameter if there is a conflict. Conversely, modern browsers generally respect the filename provided in the header, but they rely on the client-side file association rules to determine which application opens the file. If the server sends application/msword but the filename ends in .docx, some clients may fail to recognize the file type correctly or refuse to save it without an extension.

The Role of MIME Types and Extensions

In the provided code snippet, the header chain is mostly correct for forcing a download:

header('Content-Description: File Transfer');
header('Content-Type: application/msword'); // Potentially outdated for .docx
header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");
header('Content-Transfer-Encoding: binary');
// ... other headers ...
ob_clean();
flush();
readfile($doc);

The critical detail here is the mismatch between application/msword and the .docx extension. The Open XML format used by modern Word files (.docx) is technically an Office Document zipped structure, often better represented by application/vnd.openxmlformats-officedocument.wordprocessingml.document. While many servers default to application/msword for backward compatibility, this can confuse clients expecting a specific zipped archive signature.

If you rename the file using variables, the server sends the filename exactly as constructed. However, if the underlying binary data of $doc does not strictly match the MIME type declared, browsers might strip the extension or fail to prompt the save dialog correctly depending on their internal heuristics.

Updating for Modern Standards

To ensure that your dynamically generated filenames work reliably across all clients, you should align your Content-Type header with the actual file format. For .docx files, use:

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');

For Excel spreadsheets saved as .xlsx, the appropriate type is:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

This change ensures that the browser's file association logic matches the header declaration. When you combine this with a dynamic filename like $No_$Name_$Test.docx, the client receives both the correct MIME type and the explicit filename instruction, eliminating the "unknown extension" behavior.

Security Considerations: Preventing Filename Injection

When using variables to construct filenames, as seen in your example ($Name), you must guard against path traversal or injection attacks. If a user can control $Name, they might attempt to inject characters that break out of the filename context, such as ../../etc/passwd.docx.

Always sanitize and validate the variable before inserting it into the header. Using basename() helps restrict the filename to a safe string without directory traversal:

// Sanitize the name to prevent path traversal
$safeName = basename($Name);

$filename = $No . '_' . $safeName . '_' . $Test . '.docx';
header("Content-Disposition: attachment; filename=\"$filename\"");

This approach ensures that even if $Name contains malicious input, the resulting file remains within your application's control and retains the expected .docx extension. By decoupling the business logic variables from the final HTTP header construction through sanitization, you maintain both security and user experience integrity.

Our network