如何获取其他目录分支中的文件?

发布于 2024-09-29 22:56:52 字数 763 浏览 6 评论 0原文

当我尝试使用 requirerequire_once 时,如果所需的文件位于同一子目录中,但当它看到子目录之外的文件时,它会正常工作,它会产生致命错误。文件树基本上是这样的:

* main.php
+ login/
   * login.php
+ includes/
   * session.php

...所以基本上,如果我让 main.php 需要 login/login.php,那很好,但是如果我尝试对 login.php 进行目录遍历以要求 include/ session.php 失败,导致错误:

PHP Fatal error: require_once(): Failed opening required [...]

这个概念的代码是:

require('login/login.php') #works fine (main.php)
require('../includes/session.php') #quits (login.php)

我尝试使用 $_SERVER('DOCUMENT_ROOT')$_SERVER('SERVER_ADDR')dir(_FILE_)chdir(../)。我记得几个月前我在一个项目上解决了这个问题,这是一个遍历文件路径的简单函数,但我再也找不到它了。有什么想法吗?

When I try to use require or require_once, it will work fine if the file to be required is in the same subdirectory, but the moment it sees a file outside of their subdirectory, it generates a fatal error. Here is basically what the file tree looks like:

* main.php
+ login/
   * login.php
+ includes/
   * session.php

...so basically, if I were to have main.php require login/login.php, it's fine, but if I try to do directory traversal for login.php to require includes/session.php, it fails, resulting in the error:

PHP Fatal error: require_once(): Failed opening required [...]

The code for this concept would be:

require('login/login.php') #works fine (main.php)
require('../includes/session.php') #quits (login.php)

I have tried using $_SERVER('DOCUMENT_ROOT'), $_SERVER('SERVER_ADDR'), dir(_FILE_), and chdir(../). I remember working on a project a few months back that I solved this problem on, it was a simple function that traversed file paths, but I can't find it anymore. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-10-06 22:56:52

使用 set_include_path(),它会让你的生活变得更轻松。

Use set_include_path(), it'll make your life much easier.

愿与i 2024-10-06 22:56:52

这不是递归,它只是简单的旧目录遍历。

切勿对 include/require 使用相对路径。您永远不知道您的脚本是从哪个目录调用的,因此您总是有可能位于错误的工作目录中。这只是头痛的良方。

始终使用 dirname(__FILE__) 来获取当前文件所在目录的绝对路径。如果您使用的是 PHP 5.3,则只需执行 __DIR__ 即可。 (注意前后两个下划线。)

require dirname(__FILE__).'/../includes/session.php';

(顺便说一句,您不需要在 include/require 两边加上括号,因为它是一种语言构造,而不是函数。)

使用绝对路径也可能具有更好的性能,因为 PHP 不需要查看 include_path 中的每个目录。 (但这仅适用于 include_path 中有多个目录的情况。)

This ain't recursion, it's just plain old directory traversal.

NEVER use relative paths for include/require. You never know from which directory your script was invoked, so there's always a possibility that you'll be in the wrong working directory. This is just a recipe for headaches.

ALWAYS use dirname(__FILE__) to get the absolute path of the directory where the current file is located. If you're on PHP 5.3, you can just do __DIR__. (Notice two underscores front and back.)

require dirname(__FILE__).'/../includes/session.php';

(By the way, you don't need parentheses around include/require because it's a language construct, not a function.)

Using absolute paths might also have better performance because PHP doesn't need to look into each and every directory in include_path. (But this only applies if you have several directories in your include_path.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文