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 *