How can I view/open a word document in my browser using with PHP or HTML

You can use the PHP function readfile() to read the contents of a Word document and then use the appropriate headers to prompt the browser to open or download the file.

Here's an example of how you might use the readfile() function to open a Word document in the browser:


$file = 'path/to/document.docx';

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: inline; filename="' . basename($file) . '"');
header('Content-Length: ' . filesize($file));



This code sets the appropriate headers for a Word document and then uses the readfile() function to read the contents of the file and output it to the browser. The browser will then handle the file as per the headers and prompt the user to open or save it.

You can also use HTML <iframe> tag to embed the docx file in the browser.

<iframe src="path/to/document.docx" ></iframe>

Please note that in case the browser doesn't support the docx format, you can use the Google Docs Viewer to open the file.

<iframe src=""></iframe>

Make sure that the server has the right permissions to access the files.