【问题标题】:Phpspreadsheet - File does not existPhpspreadsheet - 文件不存在
【发布时间】:2020-09-09 14:43:58
【问题描述】:

我正在做一个项目,我需要读取以表格形式发送的 excel 文件,然后从中提取数据。我正在尝试使用 Phpspreadsheet 库,但 Phpspreadsheet 库似乎无法识别该文件并引发错误:该文件不存在。

$product_serial = $this->input->post('product_serial');

    $excelFilePath = base_url().'userfiles/product/excelfiles/'.$product_serial;



    try {
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($excelFilePath);
        $spreadSheet = $reader->load($excelFilePath);

        $dataAsAssocArray = $spreadSheet->getActiveSheet()->toArray();



    }
    catch (\Exception $exception){

        echo $exception->getMessage();

    }

错误:文件“http://localhost:8000/userfiles/product/excelfiles/452260670768Untitledspreadsheet.xlsx”不存在

我已经检查了上述位置的文件,该文件确实存在!

【问题讨论】:

  • 正如 Cesare 的回答所述,URL 和路径之间存在差异。访问内部资源时,您应该始终使用路径。尽管它们看起来可以互换,但它们不是

标签: php codeigniter codeigniter-3 phpspreadsheet


【解决方案1】:

Codeigniter 从文件系统读取文件,因此您必须使用 PATH 而不是 URL。

其实你使用的是网址:

base_url().'userfiles/product/excelfiles/'.$product_serial;

你需要从FCPATH构建你的路径

FCPATH.'userfiles/product/excelfiles/'.$product_serial;

有关代码点火器常量的更多信息,请点击这些链接

https://codeigniter.com/user_guide/general/common_functions.html?#global-constants

Codeigniter - dynamically getting relative/absolute path outside of application folder

【讨论】:

    猜你喜欢
    • 2018-12-02
    • 2018-10-23
    • 2020-03-17
    • 2018-10-31
    • 2018-02-15
    • 1970-01-01
    • 2019-05-18
    • 2022-01-04
    • 2020-01-03
    相关资源
    最近更新 更多