I have looked up on the site for an answer and I don't seem to be able to find a solution to my problem.
I have tried using this
$cleaned = strval(str_replace("\0", "", $string));
I have also tried:
- getcwd()
- dirname(FILE)
- basename(DIR)
however I always the the error file_exists()
expects parameter 1 to be a valid path, string given in
however I can use file_get_contents()
and it will open the document in that directory. however when I pass the docx file to be stripped to text it won't open the directory.
Here is my code:
class DocxConversion{
private $filename;
public function __construct($filePath) {
$this->filename = $filePath;
}
private function read_doc() {
$fileHandle = fopen($this->filename, "r");
$line = @fread($fileHandle, filesize($this->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\,\.\-
\t@\/\_\(\)]/","",$outtext);
return $outtext;
}
private function read_docx(){
$striped_content = '';
$content = '';
$zip = zip_open($this->filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}// end while
zip_close($zip);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "
", $content);
$striped_content = strip_tags($content);
return $striped_content;
}
public function convertToText() {
if(isset($this->filename) && !file_exists($this->filename)) {
return "File Not exists";
}
$fileArray = pathinfo($this->filename);
$file_ext = $fileArray['extension'];
echo $file_ext;
if($ext == "doc" || $ext == "docx" || $ext == "xlsx" || $ext == "pptx")
{
if($file_ext == "doc") {
return $this->read_doc();
} elseif($file_ext == "docx") {
return $this->read_docx();
} elseif($file_ext == "xlsx") {
return $this->xlsx_to_text();
}elseif($file_ext == "pptx") {
return $this->pptx_to_text();
}
} else {
return "Invalid File Type";
}
}
}
//calling class to scrape text from file
//echo $attachment['name'] . "<br />";
$filenames = file_get_contents('../api/attachmentCV/' . $attachment['name']);
//echo $filename;
$docObj = new DocxConversion($filenames);
//converting string to text
$docText= $docObj->convertToText();
echo $docText;