0

I am trying to read content of Word file. I used code below but it is giving warning as Warning: fread() [function.fread]: Length parameter must be greater than 0

$filename=$file->getFilename();
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext=='docx')
{
$fname = fopen($filename, 'r');
if (is_readable($filename)) 
{
$content = fread($fname,filesize($filename));
echo $content;
} 
else 
{
echo 'The file is not readable.';
}
fclose($fname);
}
Firdavs Kurbonov
  • 1,252
  • 4
  • 16
  • 42

3 Answers3

0

Word document isn't the same as a text file (it's more like xml / binary file), so you can't just use echo and expects it to output the file.

There's a library for that though...called Docvert

Docvert

Kylie
  • 11,421
  • 11
  • 47
  • 78
0

Try to open the file in binary mode , using the 'b' flag , like this :

fopen($filename, 'rb')
  • make sure that your path is correct , you see filesize() function use absolute path something like that C:\wamp\www\app\files\test.doc , if it's relative path the function can't find this file. – user2375682 Jul 29 '13 at 23:09
0

A docx file is binary, not text, and you'll have to read it as a binary file.

You don't state what version of word, but since your code sample used docx, I'll assume that you're using the newer Word format files.

One thing for you to be aware of is that these newer Word files, as well as Excel and PowerPoint, are stored as compressed files. The file itself is really a ZIP compatible compressed file. If you copy one of these files and add a .zip extension to it, you'll see that you can open it as a compressed file, and that you can traverse it's various folders.

What you read from the file depends on what you intend to do with it.

STLDev
  • 5,950
  • 25
  • 36