php - 无法包含文件并收到 "failed to open stream: No such file or directory"警告消息

标签 php file include centos

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

 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

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

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

下面是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';

我在 /public_html/.htaccess 中也有一个 URL Rewrite。

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

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

这是我的目录结构:

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

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

最佳答案

如果您的文件位于:

/home2/myusername/public_html/index.php

你要包含的 bootstrap.php 文件位于:

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

您的正确包含行是:

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

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

关于php - 无法包含文件并收到 "failed to open stream: No such file or directory"警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956566/

相关文章:

php - WooCommerce - 按标题获取产品?

c++ - 移动到文件中的下一行 C++

c++ - MSVC中<>和 ""的区别

C++ #include - 只有部分#include

php - pecl 安装 xdebug 版本错误

php - 在查询中返回字符串

php - xpath 中的 GET 语法

iphone - 我们可以在Xcode中以编程方式将音频文件(mp3或任何文件)切成段(较小的部分)吗?

python-3.x - Python3 : Recursively compare two directories based on file contents

c++ - 文件包含错误 (C++) : undefined reference to ______