Is there any alternative way of viewing a file(locally/online) in PHP
? Since I want to display a .docx (.doc)
file in my browser but it keeps on downloading it. So anyone who has a code for that? I keep on searching some other source code but no luck. I tried using Iframe
but still keeps on downloading the file. Thanks!

- 323
- 1
- 4
- 15

- 205
- 2
- 4
- 12
-
1You might want to read this http://stackoverflow.com/questions/188452/reading-writing-a-ms-word-file-in-php and take a look at this http://phpword.codeplex.com – bruchowski Jan 05 '14 at 04:06
-
it depends on your server and browser setting – Tun Zarni Kyaw Jan 05 '14 at 04:07
-
@TunZarniKyaw what should be the setting for that? – leonardeveloper Jan 05 '14 at 04:09
-
Convert it to pdf and show that. – Jompper Jan 05 '14 at 04:10
-
Any `code snippet` please? – leonardeveloper Jan 05 '14 at 04:11
-
@bruchowski I think PHPWord does not allow reading of Word files. You can just use it to create new files from scratch. – leonardeveloper Jan 05 '14 at 04:11
3 Answers
An easy solution to display almost every type of document in browser using iframe.
Solution is Google Docs Viewer
Just use iframe
as given below and replace url of document in it.
<iframe src='https://docs.google.com/viewer?url=ENTER URL OF YOUR DOCUMENT HERE&embedded=true' frameborder='0'></iframe>
By Replacing sample url for .docx
file, above code becomes.
<iframe src='https://docs.google.com/viewer?url=http://calibre-ebook.com/downloads/demos/demo.docx&embedded=true' frameborder='0'></iframe>
By Replacing sample url for .pdf
file, above code becomes.
<iframe src='https://docs.google.com/viewer?url=http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf&embedded=true' frameborder='0'></iframe>
Note: Document that you need to display in browser must have correct url and must be publicly available, because Google needs to fetch and display document, process fails if document is private and you will see iframe with NO PREVIEW AVAILABLE

- 16,329
- 10
- 59
- 65
I have viewed some MS Office documents on www.dropbox.com . I just had a look, the tool used is called QuickLook Framework . It converts docx/doc to pdf and displays the pdf document on the website. But it is an iOS library.
If your documents are public, you could use Office web viewer or Google Docs viewer.
I have never used PHP to view Word documents online and have never seen a complete tool for that. But writing a parser is definitely possible, the question is - how much of the formatting do you want to retain? If losing formatting is acceptable, you could try starting with answers from similar questions.
I don't know if this thread is closed or what but for google view this code works prefectly
<iframe src='//docs.google.com/gview?url=URLOFDOC.docx&embedded=true' frameborder='0'></iframe>

- 86
- 7