php PHP获取相对于站点根目录的文档路径

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

PHP getting document path relative to site root

phphtmlcss

提问by David Torrey

I'm trying to link a separate php document in the header files that contains all of the CSS link information so that if I want to change the site's design I only have to change the css path in one location (in particular for various color schemes. As I add more schemes I can just put them in a switch statement in this one file instead of going through every single page.

我试图在包含所有 CSS 链接信息的头文件中链接一个单独的 php 文档,这样如果我想更改站点的设计,我只需更改一个位置的 css 路径(特别是对于各种配色方案. 当我添加更多方案时,我可以将它们放在这个文件中的 switch 语句中,而不是遍历每一页。

I'm trying to write the code so that it will work regardless of which server it's running on (my local test server or the remote site server) without changing any path information.

我正在尝试编写代码,以便无论它在哪个服务器(我的本地测试服务器或远程站点服务器)上运行它都可以工作,而无需更改任何路径信息。

From what I was reading it seems like $_SERVER['DOCUMENT_ROOT']is the best way to find the path to the site's base folder so that I can find the server folder/css files regardless of where the page file is located.

从我阅读的内容来看,这似乎$_SERVER['DOCUMENT_ROOT']是找到站点基本文件夹路径的最佳方法,这样无论页面文件位于何处,我都可以找到服务器文件夹/css 文件。

here is an example of how I have it set up:

这是我如何设置它的示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<!--Meta Information-->

<!--CSS Info-->
<?php 
    require_once("styles/cssstyle.php");
?>

<title></title>
</head>
<body>

<!--pushes site down from top of screen -->
<div id="topmargin"></div> 

<!-- sets div for site content (puts in middle) -->
<div id="_body">          

    <div id="banner">                        
        <div class="logo"></div>      
        <div class="text"></div>
        <div class="bannerstrip"></div>
    </div>

    <!--portion for site navigation-->
    <div id="navigation">                               
        <ul class="navlinks">                            
            <li><a href="index.php">home</a></li>
        </ul>
    </div>

    <!--Holds all site usable/readable content-->
    <div id="leftwindow">                              

    </div>

    <div id="rightwindow">

    </div>

    <div id="rightwindow">

    </div>
</div>


</body>
</html>

and the CSS php file is like this:

CSS php 文件是这样的:

   <?php
echo "<link rel='stylesheet' type='text/css' href='styles/default.css'/>";
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/basicblue.css'/>";  
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/forms.css'/>";
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/loginform.css'/>";
echo "<link rel='stylesheet' type='text/css' href='" . $_SERVER['DOCUMENT_ROOT'] . "styles/newscontent.css'/>";
?>

I'm positive DOCUMENT_ROOT is set to the correct location, but my styles aren't showing up. am I missing something? Is there a more reliable way to set this up?

我确定 DOCUMENT_ROOT 设置到了正确的位置,但我的样式没有显示出来。我错过了什么吗?有没有更可靠的方法来设置它?

回答by Patrick M

Echoing what Mike said above, in my experience, $_SERVER['DOCUMENT_ROOT']is only the best option for finding files on the server. If you need php to inlcude or require something, find the server side path with DOCUMENT_ROOT.

根据我的经验,回应 Mike 上面所说的,$_SERVER['DOCUMENT_ROOT']只是在服务器上查找文件的最佳选择。如果您需要 php 来包含或需要某些东西,请使用 .php 找到服务器端路径DOCUMENT_ROOT

However, css files are client side. They're included from the relative website path. If you were to instead just do

但是,css 文件是客户端。它们包含在相对网站路径中。如果你只是做

<link rel='stylesheet' type='text/css' href='/styles/newscontent.css'/>

The opening /in the href tells the browser to always retrieve it from root of your domain: http://yourdomain.com/styles/newscontent.css.

/href 中的开头告诉浏览器始终从域的根目录检索它:http://yourdomain.com/styles/newscontent.css

回答by Nelson

You have to use $_SERVER["DOCUMENT_URI"]instead of $_SERVER["DOCUMENT_ROOT"], like this:

你必须使用$_SERVER["DOCUMENT_URI"]而不是$_SERVER["DOCUMENT_ROOT"],像这样:

echo "<link rel='stylesheet' type='text/css' href='" . dirname($_SERVER['DOCUMENT_URI']) . "/styles/basicblue.css'/>";