duanjiyun7391 2017-08-23 14:48
浏览 159

file_exists()期望参数1是有效路径,给定字符串

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;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog