php php相对路径和目录

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

php relative paths and directories

phprelative-pathdirectory

提问by Matthias

I'm wondering something, but I can't seem to be finding a good, clear answer, or even a solution to this problem:

我想知道一些事情,但我似乎无法找到一个好的、明确的答案,甚至无法解决这个问题:

My PHP website has the following structure:

我的 PHP 网站具有以下结构:

root
   functions
   generators
   helpers
   scripts
   style
   index.php

These are all folders and one php file. Functions contains a bunch op php files related to database connection and various other database operations such as inserting, deleting, updating,... Generators contains classes to automatically generate web pages to make them look all the same. Helpers are classes that handle login, logout, register, etc. Scripts are javascript and Style is CSS.

这些都是文件夹和一个 php 文件。函数包含一堆与数据库连接和各种其他数据库操作(如插入、删除、更新、... 帮助程序是处理登录、注销、注册等的类。脚本是 javascript,样式是 CSS。

In my generators folder, there is a file mainGenerator.php, this generates various pieces of the website:

在我的 generators 文件夹中,有一个文件 mainGenerator.php,它会生成网站的各个部分:

 private function generateLogin()
    {
        if (!isLoggedIn()) {
            echo "
                <h2>Login</h2>
                <form method='post' action='../helpers/login.php' id='loginForm'>
                <p>
                Username:<br/>
                <input class='search' type='text' name='username'/>
                Password:<br/>
                <input class='search' type='password' name='password'/>
                <input name='login' type='image' style='border: 0; margin: 0 0 -9px 5px;' src='style/login.png' alt='Login' title='Login'/><br/>
                No account yet? <a href='../register.php'>Register</a>
                </p></form>";
        } else {
            echo "
                <h2>Welome, <a href='user.php' style='color: #1293EE;'>" .
                 $_SESSION['user_name'] .
                 "</a></h2>
                <a href='logout.php'>Log off</a>";
        }
    }

This piece of code generates the loginbox on every page, or displays a welcome message if the user is already logged in. As you can see, the action references to "../helpers/login.php", because that's the relative location of the loginhelper from this generator's point of view.

这段代码在每个页面上生成登录框,或者如果用户已经登录则显示欢迎消息。如您所见,该操作引用了“../helpers/login.php”,因为这是相对位置从这个生成器的角度来看 loginhelper。

Now, here's the problem: If I press the login button on the index.php (http://example.com/ProjectName/index.php) it redirects me to http://example.com/helpers/login.phpand says the file isn't found.

现在,问题来了:如果我按下 index.php ( http://example.com/ProjectName/index.php)上的登录按钮,它会将我重定向到http://example.com/helpers/login.php和说找不到文件。

I see what the problem is, the post action sees the request coming from index.php and goes up one directory and then looks for /helpers/login.php, which doesn't exist of course.

我明白问题出在哪里了,post 操作看到来自 index.php 的请求并上升到一个目录,然后查找 /helpers/login.php,这当然不存在。

A fix might be to change the action to

解决方法可能是将操作更改为

action='/helpers/login.php'

but that gives me the same problem should I need access to generateLogin() from within a certain subdirectory... How can this particular problem be solved, i.e: that the reference to /helpers/login.php stays correct, no matter from where I try to access it.

但是如果我需要从某个子目录中访问 generateLogin() ,这给了我同样的问题......如何解决这个特定问题,即:对 /helpers/login.php 的引用保持正确,无论来自何处我尝试访问它。

回答by Anson

action='/ProjectName/helpers/login.php'

Assuming your root is http://localhostthen the above link should always resolve to http://localhost/ProjectName/helpers/login.phpwhether it's called from http://localhost/ProjectName/index.phpor http://localhost/ProjectName/subdir/index.php

假设你的根是http://localhost那么上面的链接应该总是解析为http://localhost/ProjectName/helpers/login.php无论它是从http://localhost/ProjectName/index.php还是http://调用的本地主机/项目名称/子目录/index.php

If you don't want to hardcode "ProjectName" into many different scripts, you can use a global variable and define it in a config file:

如果您不想将“ProjectName”硬编码到许多不同的脚本中,您可以使用全局变量并在配置文件中定义它:

helpers/ConfigOptions.php:

助手/ConfigOptions.php:

<?php
$ProjectName = "ProjectName";
?>

Then in your scripts, include the config file and use the variable you defined:

然后在您的脚本中,包含配置文件并使用您定义的变量:

index.php:

索引.php:

include $_SERVER['DOCUMENT_ROOT'] . '/helpers/ConfigOptions.php';

...

echo "
  <form action='/$ProjectName/helpers/login.php'>
  ....
";

More about DOCUMENT_ROOT:

关于 DOCUMENT_ROOT 的更多信息:

DOCUMENT_ROOTis defined in the server configuration file and is the root on the filesystem of where scripts are executing, notthe web address you would type in a browser. In the above example, I'm assuming that document root looks something like /home/user/www/ProjectName. If DOCUMENT_ROOT is only /home/user/www then you can change your include path to this:

DOCUMENT_ROOT是在服务器配置文件中定义的,是执行脚本的文件系统的根目录,而不是您在浏览器中键入的网址。在上面的示例中,我假设文档根目录类似于 /home/user/www/ProjectName。如果 DOCUMENT_ROOT 只是 /home/user/www 那么您可以将包含路径更改为:

include $_SERVER['DOCUMENT_ROOT'] . '/ProjectName/helpers/ConfigOptions.php';

or use a relative path. My vote would be on the latter since you wouldn't need to hardcode "ProjectName".

或使用相对路径。我的投票将投给后者,因为您不需要对“ProjectName”进行硬编码。

So include() and require() would take either:

所以 include() 和 require() 将采用:

  1. An absolute path to the file
  2. A path that's relative to the current executing script. If A includes B and B includes C, when B includes C it needs to provide a path that is relative to A.
  1. 文件的绝对路径
  2. 相对于当前执行脚本的路径。如果 A 包含 B 且 B 包含 C,则当 B 包含 C 时,它需要提供相对于 A的路径。

So when do you need to use DOCUMENT_ROOT? Usually with templates where a relativepath like ../helpers/file.phpcan resolve to different absolutepaths depending on what's including the file. In the above example where index.php is including ConfigOptions.php, you probably don't need to use DOCUMENT_ROOT since index.php is not a template. I used it anyway to be safe, but maybe I opened up a whole can of worms here. I hope I didn't confuse you even more.

那么什么时候需要使用 DOCUMENT_ROOT 呢?通常使用模板,其中相对路径../helpers/file.php可以解析为不同的绝对路径,具体取决于包含文件的内容。在上面 index.php 包含 ConfigOptions.php 的示例中,您可能不需要使用 DOCUMENT_ROOT,因为 index.php 不是模板。为了安全起见,我还是使用了它,但也许我在这里打开了一整罐蠕虫。我希望我没有让你更加困惑。

回答by klaustopher

If you have a link that starts with a slash it alwaysgets appended to the root and not your current directory...

如果您有一个以斜杠开头的链接,它总是会附加到根目录而不是您的当前目录...

So if your page is www.example.com/mySite/foo.htmland you have a link like this: <a href="/bar.html">Bar/<a>the user will get redirected to www.example.com/bar.html...

因此,如果您的页面是这样www.example.com/mySite/foo.html并且您有这样的链接:<a href="/bar.html">Bar/<a>用户将被重定向到www.example.com/bar.html...

You should just have your form action point to helpers/login.php

你应该让你的表单操作指向 helpers/login.php