php 使用 require_once 向上目录不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14184223/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:49:22  来源:igfitidea点击:

Using require_once for up directory not working

phpdirectoryrequire-once

提问by Yves Gonzaga

I am using require_once like this

我像这样使用 require_once

require_once('../mycode.php')

I am developing a wordpress plugin. My plugin folder is yves-slider where I have a file called yves-slider.php and a folder called admin. Inside admin folder I have a file called admin.php. I want to require file yves-slider.php in my admin.php which is located up one level directory. When I try to use

我正在开发一个 wordpress 插件。我的插件文件夹是 yves-slider,其中有一个名为 yves-slider.php 的文件和一个名为 admin 的文件夹。在 admin 文件夹中,我有一个名为 admin.php 的文件。我想在位于一级目录的 admin.php 中要求文件 yves-slider.php。当我尝试使用

require_once('../yves-slider.php')

it gives me the following error

它给了我以下错误

Warning: require_once(../yves-slider.php): failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\plugins\yves-slider\yves-slider-admin\yves-slider-admin.php on line 4

Fatal error: require_once(): Failed opening required '../yves-slider.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\wordpress\wp-content\plugins\yves-slider\yves-slider-admin\yves-slider-admin.php on line 4

警告:require_once(../yves-slider.php):无法打开流:C:\xampp\htdocs\wordpress\wp-content\plugins\yves-slider\yves-slider-admin\ 中没有这样的文件或目录第 4 行的 yves-slider-admin.php

致命错误:require_once():在 C:\xampp\htdocs\wordpress\wp-content\ 中打开所需的 '../yves-slider.php' (include_path='.;C:\xampp\php\PEAR') 失败第 4 行的 plugins\yves-slider\yves-slider-admin\yves-slider-admin.php

Am I doing wrong? I am using XAMPP 3.1, I guess that's the best way to do it.

我做错了吗?我正在使用 XAMPP 3.1,我想这是最好的方法。

回答by PeeHaa

You want to make that relative to the current path the file is in:

你想让它相对于文件所在的当前路径:

require_once __DIR__ . '/../yves-slider.php';

What probably is happening is that the current path PHP looks in is notthe path you think it is. If you are curious about what it is (the current path) you could do echo getcwd();.

可能发生的情况是 PHP 查找的当前路径不是您认为的路径。如果您对它是什么(当前路径)感到好奇,您可以这样做echo getcwd();