【问题标题】:Error including file (include_path='.:/usr/share/pear:/usr/share/php')错误包括文件(include_path='.:/usr/share/pear:/usr/share/php')
【发布时间】:2018-04-27 13:16:12
【问题描述】:

我刚刚使用slim framework 将我的项目上传到var/www 的服务器,该项目的文件夹结构如下:

include -Functions.php controller -weeklysummary.php(我要运行的脚本) vendor -autoload.php

当我尝试在Functions.php 脚​​本中调用函数时

 Warning: require(../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/project/include/Functions.php on line 11

 Fatal error: require(): Failed opening required '../vendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/innov8alert/include/Functions.php on line 11`

这就是Functions.php 的样子

class Functions {

private $conn;


function __construct() {
    require_once 'Connect.php';
    require '../vendor/autoload.php';
    require '../mailer/class.phpmailer.php';
    $db = new Connect();
    $this->conn = $db->connect();
}

function __destruct() {

}

我查了thisthis,但似乎没有任何帮助。

【问题讨论】:

  • 您是在服务器上运行composer install 还是上传了vendor 文件夹?如果没有,你需要要么做要么。
  • 文件/var/www/html/project/include/vendor/autoload.php不存在或者目录/文件权限错误。
  • 您也可以使用如下定义:define('BASE_PATH', dirname(__FILE__)); 所以您可以将整个 url 更改为如下内容:BASE_PATH _ . '/include/vendor/autoload.php'
  • @MagnusEriksson 我刚刚上传,即使是我从未使用过composer的本地项目,我只是复制并粘贴了slim,它就可以了。
  • @bassxzero 它必须在html 中?它在www 这是我第一次将项目上传到真实服务器

标签: php


【解决方案1】:

这是由于您的当前工作目录 (CWD),它与您启动脚本的目录相匹配。如果这是一个网站,应该可以安全地假设 CWD 是 index.php 文件所在的位置。

有两种解决方法:

1) 按照 cmets 的建议,在 index.php 中声明一个 BASEPATH 常量,并始终使用它作为包含路径的前缀:

// index.php:
define('BASEPATH', __DIR__.'/');

// other files:
// require BASEPATH.'path/relative/to/index.php/directory

2) 使用 __DIR__ magic constant 本身作为包含文件的前缀,但相对于您包含它们的位置(这将立即适用于您的情况,无需其他更改):

require __DIR__.'/../vendor/autoload.php';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 2014-03-05
    • 2016-10-05
    • 1970-01-01
    • 2023-03-20
    • 2018-11-14
    • 2021-03-30
    • 2019-06-07
    相关资源
    最近更新 更多