php ABSPATH 还是 __FILE__?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1960180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ABSPATH or __FILE__?
提问by Scott B
Can someone tell me if either of these two methods has an advantage over the other and why?
有人能告诉我这两种方法中的任何一种是否比另一种有优势,为什么?
$mydir = ABSPATH.'/wp-content/themes/mytheme/images';
$mydir = ABSPATH.'/wp-content/themes/mytheme/images';
$mydir = dirname(__FILE__).'/images';
$mydir = dirname(__FILE__).'/images';
They can both be used to obtain and absolute path to the images directory of "mytheme" regardless of structure of whether wordpress is installed on the root directory or in a subdirectory off the root. In both cases, they are being called from the functions.php file which is located under the "mytheme" folder.
无论wordpress是安装在根目录还是根目录下的子目录中,它们都可以用于获取“mytheme”图像目录的绝对路径。在这两种情况下,它们都是从位于“mytheme”文件夹下的functions.php 文件中调用的。
回答by Pekka
I would personally prefer dirname()as it is always guaranteed to give me the correct result, while the ABSPATH method relies on a fixed theme path and theme name that can both change.
我个人更喜欢dirname()它,因为它总是保证给我正确的结果,而 ABSPATH 方法依赖于一个固定的主题路径和主题名称,它们都可以改变。
By the way, you can use __DIR__instead of dirname(__FILE__).
顺便说一句,您可以使用__DIR__代替dirname(__FILE__).
回答by Adam
- The path to the "wp-content" directory and its subdirectories can be differentin a particular WordPress installation. Also, using the WordPress internal constants (such as
ABSPATH) is not recommended. See the Determining Plugin and Content DirectoriesWordPress Codex article. Since PHP 4.0.2, symlinks are being resolvedfor the
__FILE__and__DIR__magic constants, so take that into account.Bottom line: To determine the absolute path to a theme directory, I would suggest to use the
get_template_directory()function which also applies filtersand internally combinesget_theme_root()andget_template().
- 在特定的 WordPress 安装中,“wp-content”目录及其子目录的路径可能不同。此外,
ABSPATH不建议使用 WordPress 内部常量(例如)。请参阅确定插件和内容目录WordPress Codex 文章。 从 PHP 4.0.2 开始,符号链接正在为
__FILE__和__DIR__魔法常量解析,所以要考虑到这一点。底线:要确定主题目录的绝对路径,我建议使用
get_template_directory()也应用过滤器并在内部组合get_theme_root()和的函数get_template()。
回答by Alix Axel
For my own projects I would choose dirname(__FILE__), also there is a new constant in PHP:
对于我自己的项目,我会选择dirname(__FILE__),PHP 中还有一个新常量:
__DIR__ === dirname(__FILE__)
回答by ravs21292
ABSPATH is defined variable -> define("ABSPATH",__FILE__);
if i directly use magic constant __FILE__.it will produce same result.
ABSPATH 是定义变量 ->define("ABSPATH", __FILE__); 如果我直接使用魔术常量。__FILE__它会产生相同的结果。
In CMS ABSPATH and framework use BASEPATH is used to get root information in the form of defined variable . In the end with the help of both we get same accurate result.
在 CMS ABSPATH 和框架中使用 BASEPATH 以定义变量的形式获取根信息。最后在两者的帮助下,我们得到了同样准确的结果。

