php 无法包含文件并收到“无法打开流:没有此类文件或目录”警告消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21956566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:34:15  来源:igfitidea点击:

Cannot include file and got "failed to open stream: No such file or directory" warning message

phpfileincludecentos

提问by Sithu

I got the following error when a file is included.

包含文件时出现以下错误。

 Warning:  require(home2/myusername/public_html/inc/config.php) 
 [function.require]: failed to open stream: No such file or directory in 
 /home2/myusername/public_html/inc/bootstrap.php on line 32

 Fatal error:  require() [function.require]: Failed opening required 
 'home2/myusername/public_html/inc/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in 
 /home2/myusername/public_html/inc/bootstrap.php on line 32

It is working fine in my localhost running on Windows PC. But I got that error when I uploaded the files on my shared hosting server which is CentOS.

它在我在 Windows PC 上运行的本地主机中运行良好。但是当我将文件上传到我的 CentOS 共享托管服务器时,我遇到了这个错误。

home2/myusername/public_html/index.phpincludes /inc/bootstrap.phpwhich is then trying to include /inc/config.php. The file config.phpdoes exist on the server. home2/myusername/public_html/is returned by the function getcwd().

home2/myusername/public_html/index.php包括/inc/bootstrap.php然后试图包括/inc/config.php. 该文件config.php确实存在于服务器上。home2/myusername/public_html/由函数返回getcwd()

The below is the first few lines of codes in bootstrap.php which issues the error.

下面是 bootstrap.php 中出现错误的前几行代码。

define('APP_DIR', 'app');   

if( !defined('APP_ROOT') ){
    $APP_ROOT = trim(getcwd(), '/').'/';
    if(isset($_GET['bootstrap'])) $APP_ROOT .= APP_DIR . '/';
    define('APP_ROOT', $APP_ROOT);
}

if( !defined('ROOT') ){
    $ROOT = str_replace(APP_DIR, '', trim(APP_ROOT, '/'));
    if( strrpos($ROOT, '/') != strlen($ROOT)-1 ) $ROOT .= '/'; 
    define('ROOT', $ROOT);
}

# path to inc/ folder
define('INC', ROOT.'inc/');

# System configuration variables
require INC . 'config.php';

I have also a URL Rewrite in /public_html/.htaccess.

我在/public_html/.htaccess.

RewriteRule ^index.php$ index.php?bootstrap [L]

So, when I browse example.com/index.php, it rewrites to example.com/index.php?bootstrap. It is the situation I got the error.

因此,当我浏览时example.com/index.php,它会重写为example.com/index.php?bootstrap. 这是我得到错误的情况。

Here is my directory structure:

这是我的目录结构:

/public_html/
    |__ inc
    |   |__ bootstrap.php
    |   |__ config.php
    |__ .htaccess
    |__ index.php <--- I'm browsing this    

I think the problem would be related to the absolute path file include. The relative path file include require 'inc/bootstrap.phpin index.php is okay.

我认为问题与绝对路径文件包含有关。require 'inc/bootstrap.phpindex.php 中包含的相对路径文件是可以的。

回答by Joseph Collins

If you file is located in:

如果您的文件位于:

/home2/myusername/public_html/index.php

/home2/myusername/public_html/index.php

and you bootstrap.php file to include is located in:

并且您要包含的 bootstrap.php 文件位于:

/home2/myusername/public_html/inc/bootstrap.php

/home2/myusername/public_html/inc/bootstrap.php

your include line correctly is:

您的包含行正确是:

include_once $_SERVER['DOCUMENT_ROOT']."/inc/bootstrap.php";

include_once $_SERVER['DOCUMENT_ROOT']."/inc/bootstrap.php";

$_SERVER['DOCUMENT_ROOT'] is equal to /home2/myusername/public_html

$_SERVER['DOCUMENT_ROOT'] 等于 /home2/myusername/public_html

回答by Rimble

Most likely your paths are not correctly configured. It's an easy solution most of the time. Can you please post the code of config.php and bootstrap.php where you include the files.

很可能您的路径配置不正确。大多数情况下,这是一个简单的解决方案。您能否将 config.php 和 bootstrap.php 的代码发布到包含这些文件的位置。

It's most likely that you have to change the following.

您很可能必须更改以下内容。

/inc/config.php to config.php.

/inc/config.php 到 config.php。

Since it's in the same folder.

因为它在同一个文件夹中。

EDIT:

编辑:

You are missing a '/'

你缺少一个“/”

Failed opening required 'home2/myusername/public_html/inc/config.php' 

Should be this

应该是这个

'/home2/myusername/public_html/inc/config.php'