Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

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);

    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top