简体   繁体   中英

Error including file (include_path='.:/usr/share/pear:/usr/share/php')

I just uploaded my project to my server in var/www made using slim framework the project has a folder structure like:

include -Functions.php controller -weeklysummary.php (Script I want to run) vendor -autoload.php

When I try to call a function in the Functions.php script On my local machine the project worked fine but on the server it keeps telling me `

 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`

This is how Functions.php Looks like

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() {

}

I looked up this and this but none seemed to help.

This is due to your current working directory (CWD), which matches the one where you've started the script from. If this is a website, it should be safe to assume that CWD is where the index.php file is located.

There's 2 ways around the issue:

1) As suggested in the comments, declare a BASEPATH constant from inside index.php and always use it to prefix include paths:

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

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

2) Use the __DIR__ magic constant itself to prefix includes on files, but relative to where you include them from (this will immediately work in your case without other changes):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM