how to view doc,docx files with php???
Dani AI
Generated
A browser cannot natively render .doc/.docx. With PHP you either stream the file (the browser will download or hand off to a plugin), convert it to a web-friendly format (HTML/PDF), or use an online viewer. Your COM approach only works on Windows with Microsoft Word installed and the php_com_dotnet extension enabled, and it is fragile to run under a web server.
First, fix the immediate errors @Alberto Bucur hinted at. Use an absolute path and verify it before calling COM. Also open the document read-only, save with a full destination path, then close both the document and Word.
<?php
$src = realpath(__DIR__ . '/docs/a.doc'); // change to your real location
if (!$src || !file_exists($src)) {
die('File not found: ' . ($src ?: 'unknown'));
}
$dest = __DIR__ . '/docs/a.html'; // or .pdf if you convert elsewhere
$word = new COM('Word.Application') or die('Cannot start Word');
$word->Visible = 0;
$doc = $word->Documents->Open($src, false, true); // ReadOnly
$doc->SaveAs($dest, 8); // 8 = HTML
$doc->Close(false);
$word->Quit();
echo 'Converted to: ' . $dest; If you still see "Server execution failed", check these common causes:
- Word is not installed or bitness mismatch (32-bit PHP vs 64-bit Office) under Windows.
- The IIS/PHP user lacks permission to launch COM. In DCOMCNFG, set the Word DCOM Identity to a service account and grant Launch/Activation and file system access.
- Word needs to be run once interactively under that same account to complete first-run setup.
Production tip: Microsoft does not recommend automating Office from server code. For reliability, consider converting with LibreOffice in headless mode and then embed the PDF/HTML:
shell_exec('soffice --headless --convert-to pdf --outdir ' . escapeshellarg($outDir) . ' ' . escapeshellarg($src)); Or use a PHP library (e.g., PhpOffice PHPWord) to read .docx and produce HTML, then display that to the user.
Recommended Answers
Jump to Post— Alberto Bucur 0Set $filename from test_wordconvert.php with your file. Give all path too.
All 5 Replies
chaitu11 0 Junior Poster
wordconvert.php
<?php
<?php
require ("wordconvert.php");
new wordconvert($filename,$convert_to,$visible);
?>
/***********************IMPORTANT NOTE***********************************
ADD-> extension=php_com_dotnet.dll IN php.ini
Otherwise you will see this in your error log:
Fatal error: Class \'COM\' not found
/*************************////NOTE***********************************/
class wordconvert
{
/* variables */
var $filename; # Original File name with optional path.
# If no path provided, will be looked for in My Documents.
var $convert_to=0; # Extension (as a string) or number (See Doc on SaveAs method below),
# doc extension is the default.
var $visible=0; # 0=Hidden, 1= Visible, hidden is the default.
/* Constructor */
function wordconvert($filename,$convert_to=0,$visible=0)
{
$filename_path= substr($filename,0,-4);
if (is_string ($convert_to))
{
$convert_to= strtolower($convert_to);
switch ($convert_to)
{
case "doc":
$convert_to=0;
Break;
case "dot":
$convert_to=1;
Break;
case "txt":
$convert_to=2;
Break;
case "rtf":
$convert_to=6;
Break;
case "htm":
$convert_to=8;
Break;
case "html":
$convert_to=8;
Break;
case "asc":
$convert_to=9;
Break;
case "wri":
$convert_to=13;
Break;
case "wps":
$convert_to=28;
Break;
default:
$convert_to=0;
}
}
# Instantiate MSWord
$word=new COM("Word.Application") or die("Cannot start MS Word");
$word->visible = $visible ;
#Open the original file in word.
$word->Documents->Open($filename)or die("Cannot find file to convert");
/*
Doc on SaveAs method:
expression.SaveAs(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter)
Only the two first parameters are supported in this class.
File save format number to use ( WdSaveFormat prop. ):
wdFormatDocument = 0; wdFormatTemplate = 1; wdFormatText = 2; wdFormatTextLineBreaks = 3;
wdFormatDOSText = 4; wdFormatDOSTextLineBreaks = 5; wdFormatRTF = 6; wdFormatUnicodeText = 7;
Extensions=number: doc=0,dot =1,txt=2,htm=8,asc=9,wri=13,doc(word perfect DOS)=24,wps(works)=28
*/
#Save the new file
$word->ActiveDocument->SaveAs($filename_path,$convert_to);
/*
Doc on quit method:
expression.Quit(SaveChanges, Format, RouteDocument)
*/
$word->quit(0); #0: Quit without saving
# print "done!";
}#End of func
}#End of class
?>
test_wordconvert.php
<?php
require ("wordconvert.php"); # The class file wordconvert.php should be saved on the PHP include directory
# Change as you wish with your own values:
$filename="a.doc";
$ext = "htm"; # - will convert rtf file above to htm file
$ext = 8; # Same as above. See class info for Extension <-> Number correspondence.
$vis= 1; # =1Word will be visible, =0 to hide it
# Run the script with the above values
new wordconvert($filename,$ext,$vis); #Do the trick
print "done!";
?>
error ->Cannot find file to convert
Edited by chaitu11
Alberto Bucur 0 Junior Poster
Set $filename from test_wordconvert.php with your file. Give all path too.
chaitu11 0 Junior Poster
Warning: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Server execution failed ' in
Edited by chaitu11
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.
