简体   繁体   English

PHP警告:require_once:无法打开流:在目录中没有这样的文件或目录

[英]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 . 我所有的PHP脚本都有一个通用文件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. apache用户还具有读取权限。

Why would PHP tell me, that there was No such file ? PHP为什么会告诉我, No such file

TL;DR: The error can be thrown if an exception/error occurs within the required file. TL; DR:如果所需文件中发生异常/错误,则可能引发错误。


Actually, the error message was totally misleading. 实际上,该错误消息是完全误导的。

PHP found the file. PHP找到了该文件。 PHP compiled the file. PHP编译了文件。 PHP executed the file. PHP执行了该文件。

But the default.php also loads another file to find a class: 但是default.php还会加载另一个文件来查找类:

$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). database_wrapper使用配置(请参见下文)。 But $config['database_host'] was not set correctly. 但是$config['database_host']设置不正确。 Therefore the database connection failed. 因此,数据库连接失败。 But the only error that PHP gave me, was that there is No such file . 但是PHP给我的唯一错误是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. 在具有完全相同的PHP和apache的完全相同的计算机上的测试文件夹中,它运行得非常好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 警告:require_once 无法打开流:中没有这样的文件或目录 - PHP Warning: require_once failed to open stream: No such file or directory in 警告:require_once():无法打开 stream:没有这样的文件或目录 - Warning: require_once(): failed to open stream: No such file or directory in magento-警告:require_once(app / Mage.php):无法打开流:没有此类文件或目录 - magento - Warning: require_once(app/Mage.php): failed to open stream: No such file or directory PHP-require_once:无法打开流:没有此类文件或目录 - PHP - require_once: Failed to open stream: No such file or directory 警告:require_once(dbconnect.php):无法打开流:中没有这样的文件或目录 - Warning: require_once(dbconnect.php): failed to open stream: No such file or directory in 警告:require_once(/var/www/html/wp-config.php):无法打开流:没有这样的文件或目录 - Warning: require_once(/var/www/html/wp-config.php): failed to open stream: No such file or directory 警告:require_once(__ DIR __ / db_connect.php)[function.require-once]:无法打开流:没有这样的文件或目录 - Warning: require_once(__DIR__/db_connect.php) [function.require-once]: failed to open stream: No such file or directory 警告:require_once(includes / EliteScript.php)[function.require-once]:无法打开流:在目录中没有这样的文件或目录 - Warning: require_once(includes/EliteScript.php) [function.require-once]: failed to open stream: No such file or directory in 警告:require_once(Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php):无法打开流:没有这样的文件或目录 - Warning: require_once(Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php): failed to open stream: No such file or directory 警告:require_once(../ core / Config.inc):无法打开流:没有此类文件或目录 - Warning: require_once(../core/Config.inc): failed to open stream: No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM