1
private function read_doc($filename) {
    $fileHandle = fopen($filename, "r");
    var_dump($filename);
    $line = fread($fileHandle, filesize($filename));
    $lines = explode(chr(0x0D), $line);
    $outtext = "";
    foreach ($lines as $thisline) {
        $pos = strpos($thisline, chr(0x00));
        if (($pos !== FALSE) || (strlen($thisline) == 0)) {

        } else {
            $outtext .= $thisline . " ";
        }
    }
    $outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/", "", $outtext);
    return $outtext;
}

I am trying to read content of .doc using the above code.. but when i run the above code it give's me

filesize(): stat failed for http://localhost/jobportal/public/uploads/document/1.doc and

fread(): Length parameter must be greater than 0

Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62
  • I'm not really sure, but I think it's easier to accomplish this with COM objects. Have a look at [this answer](http://stackoverflow.com/a/19949977/). – Amal Murali Jan 26 '14 at 12:36
  • Did you have a .docx question similar to this earlier today? :) – Shomz Jan 26 '14 at 12:37
  • @Shomz Sir ,, yes i have this one...http://stackoverflow.com/questions/21362149/trying-to-read-content-of-docx-file-but-not-working – Mohammed Sufian Jan 26 '14 at 12:39

3 Answers3

1

As you can see here http://php.net/manual/en/function.filesize.php :

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

and if you will go to this link http://www.php.net/manual/en/wrappers.http.php , you will see that Supports stat() is "NO". So you can't use http links with filesize function. If this is local file just use absolute or relevant path of the file like '/path/to/my/file', if this is remote file (not sure but) I think it should be downloaded first with curl and curl_getinfo function to read Content-Length http property

Vlad Nikitin
  • 1,929
  • 15
  • 17
  • its working now the issue is with the path... thank you very much @Vlad Nikitin Sir... but just one more thing how can i display the content with formatiing... – Mohammed Sufian Jan 26 '14 at 13:01
  • 1
    You can use openoffise or libreoffise (if you are using linux) from cli using php exec function. quick tutorial http://www.techrepublic.com/blog/linux-and-open-source/how-to-convert-doc-and-odf-files-to-clean-and-lean-html/ – Vlad Nikitin Jan 26 '14 at 13:06
0

That filesize(): stat failed for http://localhost/jobportal/public/uploads/document/1.doc means: you are trying to get the filesize of an URL which isn't applicable.

You need to read from the stream in chunks until end and check how many data actually has been submitted.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
0
if($ext == 'doc'){
    if(file_exists($filename) ) {        
    if(($fh = fopen($filename, 'r')) !== false ) {
    $headers = fread($fh, 0xA00);
    $n1 = ( ord($headers[0x21C]) - 1 );
    $n2 = ( ( ord($headers[0x21D]) - 8 ) * 256 );
    $n3 = ( ( ord($headers[0x21E]) * 256 ) * 256 );
    $n4 = ( ( ( ord($headers[0x21F]) * 256 ) * 256 ) * 256 );
    $textLength = ($n1 + $n2 + $n3 + $n4);
    $content_of_file1 = fread($fh, $textLength);
    $content_of_file = strtolower($content_of_file1);

    }
}

}