【发布时间】:2013-01-24 10:39:36
【问题描述】:
我试图从不同文件夹中的 php 文件中获取我的“库”文件,但在尝试从子文件夹访问它们时会出错。比如我有这样一个目录:
+ home
- file1.php
++ subfolder
- file2.php
++ libraries
- code_generator.php
- database_library.php
code_generator.php 还依赖于其他库:
(LINE 25) require_once(realpath("./libraries/database_library.php"));
//this works fine when called from file1.php, but not from file2.php
我尝试从 ./subfolder/file2.php 调用 code_generator,例如:
chmod("../libraries/codegenerator.php", 0777); // for the error at the end of this question
require_once("../libraries/code_generator.php");
当我运行 file2.php 时,它会产生 2 个错误:
**Warning**: require_once(C:\xampp\htdocs\home\subfolder): failed to open stream: Permission denied in C:\xampp\htdocs\home\libraries\code_generator.php on line 25
**Fatal error**: require_once(): Failed opening required '' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\home\libraries\code_generator.php on line 25
*第 25 行是require_once(realpath("./libraries/database_library.php"));
【问题讨论】:
-
我猜如果你没有权限打开这个文件,你也不必chmod它。
-
@AD7six 这就是我的怀疑。 realpath() 函数从 file1.php 正确返回,但它在 file2.php 中返回空。我想我用错了。但正确的方法是什么?
-
@fedorqui 我在 realpath() 的 PHP 手册中注意到这一点:“正在运行的脚本必须对层次结构中的所有目录具有可执行权限,否则 realpath() 将返回 FALSE。”那么我将如何获得“权限”呢?
标签: php permissions directory require require-once