Php show pdf in browser

In my current project I don't want to keep PDF and DOC file on server or open due to security purpose so I want a system where only login user can view or download the files. so to do this have used given below code

Display PDF file on Browser

#############################################

$file = 'myfile.pdf';
$file1 = '/file-dir/'.$file;

ob_clean();
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/pdf");
header("Cache-Control: public");
header('Content-Disposition: inline; filename="' . $file . '"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
readfile($file1);

#############################################

Download PDF file

#############################################

$file = 'myfile.pdf';
$file1 = '/file-dir/'.$file;

ob_clean();
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/pdf");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
readfile($file1);

#############################################

Display Doc file on Browser

Their is no way you can display Doc file on browser, you can only user google reader but google reader don't open password protected files, for google reader you need to give public access file to view on browser

In this tutorial, we are going to see how to open a PDF file in browser with PHP. PHP uses standard code to display the pdf file in the web browser. The process of viewing the PDF file involves locating the PDF file on the server. It uses various types of headers to define the composition of the content in the form of Type, Layout, Transfer-Encode, etc. PHP transmits the PDF files to be able to read them on the browser. The browser displays it or downloads it from the localhost server, then displays the pdf.
 

Example 1 : How to open a PDF file in browser with PHP
  

<p></p> <h6>Example 2 : How to open a PDF file in browser with PHP</h6> <pre class="EnlighterJSRAW" data-enlighter-language="php">&lt;?php // The file path $file = "/path/to/file.pdf"; // Header Content Type header("Content-type: application/pdf"); header("Content-Length: " . filesize($file)); // Send the file to the browser. readfile($file); ?&gt;</pre> <span class="wx-related-topic-callout visible"><span class="wx-related-topic-image"><img width="800" height="445" srx="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png" data-lazy-type="image" src="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png" class="lazy lazy-hidden attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="mcq" loading="lazy" srcset="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png 800w, https://stackhowto.com/wp-content/uploads/2021/06/mcq-300x167.png 300w, https://stackhowto.com/wp-content/uploads/2021/06/mcq-768x427.png 768w" sizes="(max-width: 800px) 100vw, 800px"><noscript><img width="800" height="445" srx="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png" data-lazy-type="image" src="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png" class="lazy lazy-hidden attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="mcq" loading="lazy" srcset="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png 800w, https://stackhowto.com/wp-content/uploads/2021/06/mcq-300x167.png 300w, https://stackhowto.com/wp-content/uploads/2021/06/mcq-768x427.png 768w" sizes="(max-width: 800px) 100vw, 800px"><noscript><img width="800" height="445" srx="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="mcq" loading="lazy" srxset="https://stackhowto.com/wp-content/uploads/2021/06/mcq.png 800w, https://stackhowto.com/wp-content/uploads/2021/06/mcq-300x167.png 300w, https://stackhowto.com/wp-content/uploads/2021/06/mcq-768x427.png 768w" sizes="(max-width: 800px) 100vw, 800px"></noscript></noscript></span><span class="wx-related-topic-info"><span class="wx-related-topic-title"><a target="_blank" href="https://stackhowto.com/mcq/">MCQ</a></span><span class="wx-related-topic-excerpt">Practice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.</span><a target="_blank" href="https://stackhowto.com/mcq/">Read More</a></span></span> <div class="adsbyvli" style="width:728px;height:90px" data-ad-slot="vi_2249496259"></div> <script>(vitag.Init = window.vitag.Init || []).push(function () { viAPItag.display("vi_2249496259") })