hi
the below code is used to read a uploaded file and print the first three lines in a document,
it is working fine in a txt file .i have written the else part for doc file....it is working but junk values are also getting printed

<?php
$lines=file('http://www.wb.com/judson.doc');
$lin = "/home/wb/public_html/judson.doc";
$extension = array_pop(explode(".", $lin));
 if (($extension == "txt"))
{
foreach ($lines as $line_num => $line) {
if ($line_num >=0 && $line_num <= 3) {
    echo "<a href='http://www.wb.com/judson.txt'>" . htmlspecialchars($line) . "</a><br />\n";
}
}
}
else
{
  $fileHandle = fopen($lin, "r");
    $line = @fread($fileHandle, filesize($lin));   
    $lines = explode(chr(0x0D),$line);
    $outtext = "";
foreach ($lines as $line_num => $line) {
if ($line_num >=0 && $line_num <= 3) {
$line = preg_replace("/[^a-zA-Z0-9\s\,\.\-@\/\_\(\)]/","",$line);
    echo "<a href='http://www.wb.com/judson.doc'>" .$line. "</a><br />\n";
}
}
}?>

You cant read a DOC file in the manner that you have used as the content is not stored as in the case or normal text file.

Try opening the doc file with notepad and you will get what i mean.

You are better off using iFilter for reading DOC files.

This article has been dead for over six months. Start a new discussion instead.