19

How can I open and view a .doc file extension in my browser? The file is located on my server.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Ruben
  • 8,956
  • 14
  • 63
  • 102

6 Answers6

20

Two options: First is to just link to it, e.g. <a href="MyWordDocument.doc">My Word Document</a>, the second is to use an iframe and point it to the document. For this to work, however, most browsers require that the server sends a Content-disposition: inline header with the document. If you cannot configure your web server to do this, you can wrap the document in a bit of php:

<?php
header('Content-disposition: inline');
header('Content-type: application/msword'); // not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;

And then link to that script instead of your word document.

This isn't guaranteed to work though; the content-disposition header is just a hint, and any browser may choose to treat it as an attachment anyway.

Also, note that .doc isn't exactly portable; basically, you need Word to display it properly (Open Office and a few other Open Source applications do kind of a decent job, but they're not quite there yet), and the browser must support opening Word as a plugin.

If the .doc file format requirement isn't set in stone, PDF would be a better choice (the conversion is usually as simple as printing it on a PDF printer, say, CutePDF, from inside Word), or maybe you can even convert the document to HTML (mileage may vary though).

tdammers
  • 20,353
  • 1
  • 39
  • 56
  • 1
    It is true that open office is not 100% accurate at displaying `.doc` files, but the same can be said of MS-Word. I have had about the same failure rate for both; It is the `.doc` format that is the problem. – ctrl-alt-delor Oct 25 '15 at 14:24
  • how can I create something editable, like once opened if someone makes changes in that it gets updated on the server? – PHP Avenger Jan 11 '19 at 16:25
6
<a href="foo.doc">…</a>

You will need a browser with a plugin for Office documents installed. I believe Microsoft Office will install one for at least Internet Explorer by default.

If you want to work without a plugin, then you will need to convert the document to another format — HTML for maximum compatibility. This isn't a trivial operation, especially for complex documents (or even those which just contain images).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
5
$file = "$file_name.doc";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type:application/zip"); // Send type of file
$header="Content-Disposition: attachment; filename=$patient_name.zip;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
emeka
  • 59
  • 1
  • 1
5

You can use google docs instead as it is free and reliable You can assign your file path to iframe.

e.g. iframe1.Attributes.Add("Src", "http://docs.google.com/gview?url=http://YOUR_FILE_PATH&embedded=true");

Hardik Mali
  • 59
  • 1
  • 2
4

If your .doc file is accessable online, you can try Office Web Viewer service.

If your documents stored in Intranet, you can use Microsoft Office Web Apps Server. It allows users to view Word, PowerPoint, Excel documents via browser.

yesnik
  • 4,085
  • 2
  • 30
  • 25
0
//Edit
$header="Content-Disposition: attachment; filename=$file_name.doc;"; // Send File Name
emeka
  • 59
  • 1
  • 1
  • 4
    Thanks for the feedback and proposed solution. I'm sure the OP would appreciate some elaboration on how to use this and what it does. – Felix Frank Aug 15 '14 at 10:47
  • 2
    This doesn't answer the question. This will download the file (with other header params included) – Dut A. Feb 23 '16 at 18:38