dongyi9082
2012-12-07 18:29
浏览 80
已采纳

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

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.

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.

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:

   <?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?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题