Force a PDF file to open in web browser instead of download in PHP

Normally web browser decides a file to be downloaded or opened within a tab based on various types of headers a programming language passes. These headers define content composition in form of type, Disposition, Transfer-Encoding.

So PHP doesn’t actually read the file but send signals for web browser to render it. Here is how it looks when using PHP to display PDF file on Chrome.

header('Content-type: application/pdf');  
header('Content-Disposition: inline; filename="my_file.pdf"'); 
header('Content-Transfer-Encoding: binary'); 
header('Accept-Ranges: bytes'); 
@readfile('my_file.pdf'); 

or

$filename = "/path/to/my_file.pdf";   
header("Content-type: application/pdf");  
header("Content-Length: " . filesize($filename)); 
readfile($filename); 

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close