如何从不同的目录require_once? [英] How to require_once from different directories?

查看:169
本文介绍了如何从不同的目录require_once?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从不同文件夹的php文件中要求我的库文件,但是尝试从子文件夹访问它时会产生错误。例如我有一个目录:

  + home 
- file1.php

++子文件夹
- file2.php

++库
- code_generator.php
- database_library.php
/ pre>

code_generator.php 也取决于其他库:

 (第25行)require_once(realpath(./ libraries / database_library.php)); 
//这可以从file1.php调用,但不是来自file2.php

我尝试从./subfolder/ file2.php 调用code_generator,如:

  chmod( ../libraries/codegenerator.php,0777); //对于这个问题的结尾的错误
require_once(../ libraries / code_generator.php);

当我运行file2.php时,会产生2个错误:

  **警告**:require_once(C:\xampp\htdocs\home\subfolder):无法打开流:C:中的权限被拒绝在线25上的\\ xampp\htdocs\home\libraries\code_generator.php 

**致命错误**:require_once():打开必需的''(include_path ='; C :\xampp\php\PEAR')在C:\xampp\htdocs\home\libraries\code_generator.php第25行

*第25行是 require_once(realpath(./ libraries / database_library.php));

解决方案

该文件不存在>

错误消息如下:


警告:require_once(C:\xampp\htdocs\home\\ \\ subfolder):无法打开流:


And:




无法打开所需的''


错误消息永远不会(好吧,几乎从未),所以你 调用:

  require_once(''); 



Realpath对于缺少的文件返回false



如果您将路径传递给不存在的文件,Realpath将返回false:

  $ return = realpath('this /doesnt/exist.foo'); // $ return is false 

因此,传递给realpath的参数不指代文件,这是路径不起作用的原因。



不要超过使用realpath



如果您删除使用realpath,你会发现错误信息很明显。猜测你试图包含 C:\xampp\htdocs\home\subfolder\libraries\database_library.php - 因为有一个缺失


第25行是 require_once realpath(./ libraries / database_library.php));


如果你只是定义一个根路径:

  define('ROOT','C:\xampp\htdocs\home\\' ); 

然后你可以绝对地使用这个常数,这些问题不会发生:

  require_once(ROOT。libraries / database_library.php); 

如果您的路径错误,错误消息将显示出问题所在。


I am trying to require my "library" files from php files in different folders, but it gives errors when trying to access them from a subfolder. For example I have such a directory:

+ home    
- file1.php

 ++ subfolder
  - file2.php

 ++ libraries
  - code_generator.php
  - database_library.php

code_generator.php also depends on the other library:

(LINE 25) require_once(realpath("./libraries/database_library.php"));
//this works fine when called from file1.php, but not from file2.php

I try to call code_generator from ./subfolder/file2.php like:

chmod("../libraries/codegenerator.php", 0777);  // for the error at the end of this question
require_once("../libraries/code_generator.php");

When I run file2.php, it yields 2 errors:

**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

*line 25 is require_once(realpath("./libraries/database_library.php"));

解决方案

The file does not exist.

The error message reads:

Warning: require_once(C:\xampp\htdocs\home\subfolder): failed to open stream:

And:

Failed opening required ''

Error messages never lie (well, almost never), therefore you are calling:

require_once('');

Realpath returns false for missing files

Realpath will return false if you pass it a path to a file that doesn't exist:

$return = realpath('this/doesnt/exist.foo'); // $return is false

Therefore, the argument passed to realpath doesn't refer to a file, which is the reason the path doesn't work.

Don't over use realpath

If you remove the use of realpath, you'll find that the error message is probably obvious. At a guess you're trying to include C:\xampp\htdocs\home\subfolder\libraries\database_library.php - because there is a missing . in this:

line 25 is require_once(realpath("./libraries/database_library.php"));

If instead you just define a root path:

define('ROOT', 'C:\xampp\htdocs\home\\');

Then you can use this constant with absolute confidence that these sort of problems wont occur:

require_once(ROOT . "libraries/database_library.php");

And if you do get the path wrong, the error message will make it obvious what the problem is.

这篇关于如何从不同的目录require_once?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆