Example 2 on w3schools shows what you are trying to achieve.

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename=\"downloaded.pdf\"");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

Also remember that,

It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem)

Answer from gat on Stack Overflow
🌐
PHP
php.net › manual › en › function.header.php
PHP: header - Manual
For example: <?php header('WWW-Authenticate: Negotiate'); header('WWW-Authenticate: NTLM', false); ?> ... Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the header is not empty.
🌐
W3Schools
w3schools.com › php › func_network_header.asp
PHP header() Function
Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box): <?php header("Content-type:application/pdf"); // It will be called ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php header()
PHP header() | Complete Guide to PHP header() with Examples
March 31, 2023 - Explanation: In this example, we are prompting the user to save the generated PDF file being sent to them. For this purpose, we use the Content-Disposition header to give a required file name and to force the web browser to show the save dialog.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
W3Docs
w3docs.com › php
correct PHP headers for pdf file download
<?php // Set the appropriate headers header("Content-Type: application/pdf"); header("Content-Disposition: attachment; filename=document.pdf"); // Read the file content and output it readfile("document.pdf");
🌐
PHPpot
phppot.com › php › php-header
PHP header - PHPpot
PHP header and related functions syntax, usage with simple examples for setting the request and metadata via header.
🌐
Pi My Life Up
pimylifeup.com › home › how to use the php header function
How to Use the PHP header Function - Pi My Life Up
November 4, 2024 - <?php header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");Copy · The example below is the response we received from our local web development server. In this example, we show you how to specify a file for a user to download. We first indicate the content type, which is a PDF file.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-open-a-pdf-files-in-web-browser-using-php
How to open a PDF files in web browser using PHP? - GeeksforGeeks
July 11, 2025 - '"'); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); // Read the file @readfile($file); ?> ... Example : This PHP script displays a PDF in the browser by specifying its server path.
Find elsewhere
🌐
W3Schools
w3schools.in › php › php-headers
PHP Header Function - W3Schools
<?php //Browser will deal page as PDF header ( "Content-type: application/pdf" ); //myPDF.pdf will called header ( "Content-Disposition: attachment; filename=myPDF.pdf' " ); ?>
🌐
Simplilearn
simplilearn.com › home › resources › software development › header in php: the ultimate guide to header function
Header in PHP: The Ultimate Guide to Header Function
September 11, 2025 - The header in PHP is a built-in function for sending a raw HTTP header. Learn how to 📥 download files using a header function and much more. Start now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
GitHub
gist.github.com › irazasyed › 5788118
PHP HTTP Headers (Examples) · GitHub
PHP HTTP Headers (Examples) Raw · headers.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
GitHub
gist.github.com › reinink › 2504638
PHP header examples · GitHub
PHP header examples. GitHub Gist: instantly share code, notes, and snippets.
🌐
OneLinerHub
onelinerhub.com › php-tcpdf › how-do-i-add-a-header-to-a-pdf-file-using-php-and-tcpdf
Php Tcpdf: How do I add a header to a PDF file using PHP and TCPDF? - OneLinerHub
<?php // Include the TCPDF library require_once('tcpdf_include.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); ...
🌐
Scaler
scaler.com › home › topics › php header() function
PHP header() Function - Scaler Topics
April 12, 2024 - Example: header('Content-Disposition: attachment; filename="file.pdf"') Used for specifying the behavior of the content being sent. For example, an attachment suggests that the content should be treated as a file attachment, and a filename specifies ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-header-function
PHP | header() Function - GeeksforGeeks
July 11, 2025 - Note: The header() functions is used multiple time in the example as one header is allowed to send at a time (since PHP 4.4) to prevent header injection attacks.
Top answer
1 of 7
29

Is there a DEFAULT header? If I just send some PHP to a browser without specifying a header.. what header will it apply to it?

There are lots of different HTTP headers that mean different things. PHP will give you defaults for the important ones if you don't set them yourself.

I think the header you're specifically talking about is Content-Type. This tells the browser what kind of file you're sending it. If you say text/html, it will try to display what you give it as a web page. If you say application/pdf, it'll try to display or download it as a PDF file.

PHP defaults to sending Content-Type: text/html. If that's all you want, you don't have to call header('Content-Type: ...'); at all. However, if you are using any non-ASCII Unicode characters, you may wish to set Content-Type to text/html;charset=something, where something is the encoding you're using for them (often, utf-8). Otherwise the browser will have to guess and might get it wrong. The commonly-seen <meta http-equiv="Content-Type" content="text/html;charset=something"/> tag is an alternative way of doing the same thing; if you want to be really safe about it, you can use both.

If you serve a JPEG image as text/html, which is what will be happening if you follow “someone”'s questionable advice by removing the header() call, then going to the URL of the image in the browser will try to display the binary image as HTML, which will give you a big old load of garbage on the screen. That's not very good, really.

However in many browsers, such a broken JPEG will still usually work when you point an <img src> tag at it. This is because when you use an <img>, the browser knows it's going to be fetching an image, and ignores you when you say it's actually HTML. It then has to to ‘sniff’ the contents of the file to see whether it looks like a JPEG, a GIF, a PNG, or some other kind of image it knows about, so it knows how to display it. Browsers have done this because there are so many poorly-written sites out there that forget to send the header. Boo!

So definitely send header('Content-Type: image/jpeg') when you're writing a JPEG, or any other non-HTML type. For HTML pages, you can often get away without it.

2 of 7
11

Headers are not specific to browsers, it's a part of the HTTP protocol.
A request for a page (or any other resource like images) will cause the client (e.g. Internet browser) to send a request header. This could contain an header for language (Accept-Language) for example.

The first line of a HTTP request is in the format METHOD RESOURCE HTTP/VERSION. Example: GET /resource HTTP/1.0.
HTTP/1.1 requires the Host-header. An example HTTP/1.1 request:

GET / HTTP/1.1
Host: example.com

The server responds with at least a status code: HTTP/1.1 200 OK
Most servers will send additional headers. Common headers are: Content-Type, Date, Server and Content-Length.

This is an example request (raw data):

$ nc example.com 80
GET / HTTP/1.0

HTTP/1.1 200 OK
Date: Sat, 11 Sep 2010 19:12:13 GMT
Server: Apache
Last-Modified: Fri, 30 Jul 2010 15:30:18 GMT
ETag: "573c1-254-48c9c87349680"
Accept-Ranges: bytes
Content-Length: 596
Connection: close
Content-Type: text/html; charset=UTF-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
  <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  <TITLE>Example Web Page</TITLE>
</HEAD> 
<body>  
<p>You have reached this web page by typing &quot;example.com&quot;,
&quot;example.net&quot;,&quot;example.org&quot
  or &quot;example.edu&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not available 
  for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 
  2606</a>, Section 3.</p>
</BODY>
</HTML>

It's up to the client (Internet browser) whether to parse a header or not. All modern Internet browsers parses the Content-Type header, and use it to determine how to display a resource (is it a HTML page, an image, a text file or something else?). The Server header is ignored by browsers, servers use it to identify themselves. But some crawler might use it for statistics.

A quote from the HTTP specification:

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma.

That means that multiple Content-Type fields are not valid, and the behaviour is undefined (although it's common to use the last defined one).

This Wikipedia article contains a list of headers with a description.

🌐
Tutorialspoint
tutorialspoint.com › php › php_function_header.htm
PHP Network header() Function
Below is the syntax of the PHP ... − · $header − (Required) The exact header text to send, like "Location: URL" or "HTTP/1.1 404 Not Found."...