0

I want to extract text from docx and doc file. I am using this class The one in the answer.

Everything works fine when i use them in native php and docx file in the same directory of the php files. It extracts pretty well. This is not the case when i upload them through <input type="file">. You can see in the link that this class accepts only docx,doc,pptx and xlsx. I know when you upload file in php it renames and move to temp to avoid name clashing and overwriting. So i came with something like getting the tmp file and removing its extension and adding docx or doc to it. Here is my code

$file = $request->file('resume');
echo $file."<br>";
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$echo $withoutExt."<br>";
$original_file = $withoutExt.".docx"."<br>";
$echo $original_file."<br>";
$doc_file = new DocxConversion($original_file);
$echo $docText= $doc_file->convertToText();

The above code gives me the output as i expected till converting the .tmp to .docx but finally says File Not exists Here is the output

C:\xampp\tmp\phpCB7E.tmp
C:\xampp\tmp\phpCB7E
C:\xampp\tmp\phpCB7E.docx

File Not exists

I have also tried to put a docx file in the controllers directory and tried to execute like this

public function index1(){
echo "hello";
$docObj = new DocxConversion("hello.docx");
var_dump($docText= $docObj->convertToText());
}

The above approach also says File not exists. Am i doing anything wrong here? It works perfectly with the same file in native code where my php files and docx files are in same directory but not when i use it in my controller.

Community
  • 1
  • 1
Naveen Kumar
  • 1,476
  • 5
  • 28
  • 52

1 Answers1

0

Assuming your $file is a UploadedFile you can use the getRealPath method to get the path to the filename,

$file = $request->file('resume');
$doc_file = new DocxConversion($file->getRealPath());
echo $doc_file->convertToText();
Moak
  • 12,596
  • 27
  • 111
  • 166