KiranKumar Posted May 9, 2007 Share Posted May 9, 2007 Hi, I'm new to PHP, can any one tell me how I can display the content of a MS-Word doc on a webpage ? Thanks in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/50643-php-how-to-read-ms-word-doc/ Share on other sites More sharing options...
jitesh Posted May 9, 2007 Share Posted May 9, 2007 <?php // get contents of a file into a string $filename = "document.doc"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50643-php-how-to-read-ms-word-doc/#findComment-248921 Share on other sites More sharing options...
KiranKumar Posted May 9, 2007 Author Share Posted May 9, 2007 Thanks for ur reply, I tried your solution but it doesn't work. It shows all junk characters when I open my webpage. How to solve this problem ? Quote Link to comment https://forums.phpfreaks.com/topic/50643-php-how-to-read-ms-word-doc/#findComment-248926 Share on other sites More sharing options...
redbullmarky Posted May 9, 2007 Share Posted May 9, 2007 the method i use is very similar to what jitesh mentions. without knowing the exact file format's structure, it's very hit and miss - if it's important to keep the formatting in tact, then you're going to struggle a bit unless you're prepared to get down and dirty and do some serious homework. in addition to jitesh's code, put this line: $header = fread($handle, 2560); just before the $contents = fread() line, which will read off the first 2560 bytes (which is essentially the header and useless without knowing what to do with it...) what you're left with then (in $contents) is the plain text with various formatting characters (ie, the 'Junk' you mention). a series of str_replace / preg_replace will deal with these. there are programs such as antiword / catdoc, etc you can google for (both free) which will take the headache out of it, but like i say - if you want it to appear in your browser as it would in MS Word itself, then you'd need a plugin for your browser, rather than anything specifically PHP Quote Link to comment https://forums.phpfreaks.com/topic/50643-php-how-to-read-ms-word-doc/#findComment-248929 Share on other sites More sharing options...
redbullmarky Posted May 9, 2007 Share Posted May 9, 2007 as i'm generous: catdoc: http://www.45.free.net/~vitus/software/catdoc/ antiword: http://www.winfield.demon.nl/ Quote Link to comment https://forums.phpfreaks.com/topic/50643-php-how-to-read-ms-word-doc/#findComment-248932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.