PHP将包括与调用脚本相关的文件,但如果include路径包含"../",则不包括该文件. [英] PHP will include a file relative to a calling script, but not if the include path contains "../"

查看:78
本文介绍了PHP将包括与调用脚本相关的文件,但如果include路径包含"../",则不包括该文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我从PHP文档和此处的其他文章中收集到的信息,PHP的include(和include_once)执行以下操作:

From what I gather from PHP's documentation and from other posts here, PHP's include (and include_once) do the following:

基于给定的文件路径或如果没有给定的include_path包含文件.如果在include_path中找不到该文件,则include最终将在失败之前检入调用脚本的自身目录和当前工作目录

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing

我在给定目录中具有以下结构:

I have the following structure in a given directory:

  • index.php
  • /dirA(包含a.php和b.php)
  • /dirB(包含c.php)

来自index.php include_once "dirA/a.php"

From index.php include_once "dirA/a.php"

这是在a.php中起作用的内容:
include_once "b.php"
include_once "dirB/c.php"

Here's what works from within a.php:
include_once "b.php"
include_once "dirB/c.php"

这是在a.php中不起作用的内容:
include_once "b.php"
include_once "../dirB/c.php"

Here's what DOESN"T work from within a.php:
include_once "b.php"
include_once "../dirB/c.php"

令我感到奇怪的是,相对于调用脚本自己的目录"包含了b.php,但是仅相对于当前工作目录(包含index.php的目录)才考虑了c.php.对我来说,这似乎有点矛盾. PHP将包含与调用脚本相关的文件,但如果包含路径包含../,则不会包含该文件-为什么? 为什么../父目录指令相对于调用脚本不起作用,而相对于当前工作目录呢?(请注意:我相对于cwd对其进行了测试,但未包含该指令)我上面的示例中的文件只是为了使其更干净.它工作得很好)

The curious thing to me is that b.php is included relative to the "calling script's own directory" but c.php is only considered relative to the current working directory (which is the dir containing index.php). This seems to be a slight inconsistency to me. PHP will include a file relative to a calling script, but not if the include path contains ../ - why? Why won't the ../ parent directory directive work relative to the calling script but it will relative to the current working directory? (note: I tested it relative to the cwd but didn't include that file in my example above just to keep it cleaner. It worked just fine)

有人能解释为什么会这样吗? 应该以这种方式工作还是这是一个错误?

Can anyone shed some light as to why this is? Should it work this way, or is this a bug?

推荐答案

PHP查找文件的方式有点奇怪.如果包含名称以斜杠或点开头的文件,则PHP会完全忽略include_path.如果是一个点,则假定该名称是相对于启动所有内容的脚本(即,网络服务器决定运行的脚本)的相对名称.

PHP's a bit odd in how it looks for files. If you include a file whose name starts with a slash or a dot, PHP ignores the include_path entirely. If a dot, it assumes the name is relative to the script that kicked off everything (ie: the one the web server decided to run).

如果要指定相对于所包含脚本而不是启动脚本的路径,则您真正想要的是一个绝对包含,用于指定文件的全名.使用__FILE____DIR__常量很容易做到,它们反映了当前正在运行的脚本的名称和目录.

If you want to specify a path relative to the included script and not the startup one, what you really want is an absolute include that specifies the full name of the file. That's easy to do with the __FILE__ and __DIR__ constants, which reflect the name and directory of the currently running script.

include __DIR__ . '/../dirB/c.php';

如果愿意,还可以设置include_path配置设置以包括应用程序的根目录,而只需指定相对于它的所有文件名即可.

If you like, you can also set the include_path config setting to include the root of the app, and just specify all filenames relative to that.

这篇关于PHP将包括与调用脚本相关的文件,但如果include路径包含"../",则不包括该文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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