dongxiezhi9564
dongxiezhi9564
2016-07-27 07:31
浏览 165
已采纳

require_once(PHPExcel / Classes / PHPExcel.php):无法打开流

I am trying to include PHPExcel to a Silverstripe 3 site to export excel sheets. Right now I am just trying to test, but I get this error when trying to do it:

[Warning] require_once(/sitename/mysite/AddOns/PHPExcel/Classes/PHPExcel.php): failed to open stream: No such file or directory

Thing is I know this file exists since I copied it over myself and have rechecked the path over and over. So I decided "well check if the file exists" using this code:

    if(!file_exists(Director::baseURL().'mysite/AddOns/PHPExcel/Classes/PHPExcel.php')) {
        echo 'sdf';exit;
}

The path is correct (that is where it is saved) according to the error, but- file does not exist. I am also requiring the file in the same way, with no luck

require_once Director::baseURL().'mysite/AddOns/PHPExcel/Classes/PHPExcel.php';

I have tried everything-checking file permissions, referencing parent folders using ../../, calling it directly like AddOns/PHPExcel, moving it to this new AddOns folder (first tried placing the PHPExcel classes on the root and discovered that Silverstripe doesn't read it then :) )

I know I am doing something wrong but for the life of me I cannot see what. Please help

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • ds3422222
    ds3422222 2016-07-27 10:25
    已采纳

    BASE_PATH is the best way to access the web root folder.

    require_once(BASE_PATH . '/AddOns/PHPExcel/Classes/PHPExcel.php');
    

    Also this is only an issue if you are not using composer, to solve this issue in the correct way you should use composer.

    点赞 评论
  • dsdtumf776629385
    dsdtumf776629385 2016-07-27 08:56

    As pointed out Director::baseURL() will return the URL rather than the filepath.

    Instead require relative to the file web root like so:

    require_once(BASE_PATH . '/AddOns/PHPExcel/Classes/PHPExcel.php');

    As pointed out by both Dan and Barry in the other answers, it's preferable to use composer for dependency management.

    点赞 评论
  • duanrong3308
    duanrong3308 2016-07-27 10:42

    You should consider using composer to include the PHPExcel class, this will avoid the need to manually require the class and will help you with dependency management.

    composer require phpoffice/phpexcel
    
    点赞 评论

相关推荐