November 6, 2026?2 min

Content-Disposition: Why Your Downloads Open in the Browser

A small HTTP header solves a surprisingly common problem: browsers playing files instead of saving them.

PHPArchitectureOpen Source

I hit this wall building file management features for a client's ERP migration. They needed to export invoices as PDFs. Simple enough—fetch the file, send it to the browser. Except Chrome opened it in a tab every time.

The fix? One header: Content-Disposition: attachment; filename="invoice.pdf"

Without it, browsers treat most file types as "display this." PDFs, images, even some videos get opened inline. Add that header, and the browser switches to "save this" mode.

I see this pattern everywhere now. File downloads in Seven Shop, export features in legacy system migrations, even Telegram bot file handlers need this. It's not optional—it's table stakes for user-facing file operations.

Here's the thing: the header is simple, but people skip it. They're focused on getting the file to the browser, not controlling what the browser does with it.

header('Content-Disposition: attachment; filename="report.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile($filepath);

That's it. Two headers. The filename in the disposition header becomes what shows up in the save dialog.

One caveat: inline vs attachment. Use inline if you want browsers to display the file if they can (good for PDFs in some cases). Use attachment to force download. I usually default to attachment because users know what they're getting.

Also set Content-Length if the file is static. It lets browsers show accurate download progress.

This matters more than it sounds. Poor download UX frustrates users. They might think the file didn't work, that your app is broken. It's a tiny detail that separates "feels polished" from "feels janky."

When you're modernizing legacy systems or building new features, these small HTTP details compound. Get them right early.