简体   繁体   中英

PHP Warning: require_once: failed to open stream: No such file or directory in

All of my PHP scripts have a common file default.php .

That file is located at the root folder of my application. It defines some constants, variables, functions and error-handling. It also loads the config file.

After my last merge into the production server I saw a strange behavior.

$sudo -u apache php /var/www/html/PDR/src/php/pages/menu-tiles.php 

PHP Warning:  require_once(../../../default.php): failed to open stream: No such file or directory in /var/www/html/PDR/src/php/pages/menu-tiles.php on line 18
PHP Fatal error:  require_once(): Failed opening required '../../../default.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/PDR/src/php/pages/menu-tiles.php on line 18

But the file was there:

ls -la /var/www/html/PDR/src/php/pages/../../../default.php
-rw-r--r-- 1 apache apache 4554  1. Jun 00:44 /var/www/html/PDR/src/php/pages/../../../default.php

The apache user also had read permissions.

Why would PHP tell me, that there was No such file ?

TL;DR: The error can be thrown if an exception/error occurs within the required file.


Actually, the error message was totally misleading.

PHP found the file. PHP compiled the file. PHP executed the file.

But the default.php also loads another file to find a class:

$List_of_branch_objects = branch::read_branches_from_database();

That class loads another class to query the database.

$result = database_wrapper::instance()->run($sql_query);

The database_wrapper uses the configuration (see below). But $config['database_host'] was not set correctly. Therefore the database connection failed. But the only error that PHP gave me, was that there is No such file .

public static function instance() {
    if (self::$instance === null) {
        self::$instance = new self;
    }
    return self::$instance;
}
function __construct() {
    global $config;
    $options = array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
        PDO::ATTR_EMULATE_PREPARES => FALSE,
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
    );
    $this->database_host = $config['database_host'];
    $this->database_name = $config['database_name'];
    $this->database_port = $config['database_port'];
    $this->database_user_name = $config['database_user'];
    $this->database_password = $config['database_password'];
    if (!empty($this->database_port) and 3306 != $this->database_port) {
        $port_string = 'port=' . $this->database_port . ';';
    } else {
        $port_string = '';
    }
    $dsn = 'mysql:host=' . $this->database_host . ';' . $port_string . 'dbname=' . $this->database_name . ';charset=utf8';
    $this->pdo = new PDO($dsn, $this->database_user_name, $this->database_password, $options);
}

It was a nightmare for me to find the source of the error, because it was on the production server. It had run perfectly fine in the testing folder on the exact same machine with the exact same PHP and apache.

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