donglu5047 2016-12-31 14:09
浏览 128

如何使用原始上传文件而不使用Laravel中的.tmp文件

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.

  • 写回答

1条回答 默认 最新

  • dongling4383 2016-12-31 17:40
    关注

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了