包含文件时出现以下错误。
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.php
。 home2/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/