require() fails even though file_exists() claims file is there(即使 file_exists() 声明文件存在,require() 也会失败)
问题描述
这是我的代码:
if (file_exists('config.php')) {
require('config.php'); // This is line 38
}
以某种方式产生错误:
警告:require(config.php) [function.require]:打开流失败:第 38 行的/path/name/file.php 中没有这样的文件或目录
Warning: require(config.php) [function.require]: failed to open stream: No such file or directory in /path/name/file.php on line 38
这怎么可能?
更新:以下确实工作:
if (file_exists(getcwd(). '/config.php')) {
require(getcwd(). '/config.php');
}
推荐答案
尝试忽略包含路径:
if (file_exists('config.php')) {
require('./config.php'); // This is line 38
}
如果它有效,你就错过了.目录到包含路径中,您必须选择包含它或使用相对路径文件名
if it works you are missing . directory into the include path and you have to choose to include it or using relative path file names
您可以使用 php 配置指令更改您的 include_path(如果您可以更改 php 配置文件)或使用 get_include_path() 和 set_include_path()文件/项目库
You can change your include_path with a php configuration directive (if you can change the php config file) or resort to get_include_path() and set_include_path() on a per file/project base
ex: set_include_path('.'. PATH_SEPARATOR . get_include_path());
在你的 php 的第一行(或在一个通用的配置文件中);
ex: set_include_path('.'. PATH_SEPARATOR . get_include_path());
in your first line of php (or in a common configuration file);
来源:
- 包含php手册
- include_path
- set_include_path
- get_include_path
这篇关于即使 file_exists() 声明文件存在,require() 也会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:即使 file_exists() 声明文件存在,require() 也会失败


基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01