我正在使用一个简单的解决方案来获取服务器根目录,它与我的包含文件配合得很好:
<?php $root = $_SERVER['DOCUMENT_ROOT'].'/frizkie'; ?>
我正在做一些表,并且包含该表的 .php 文件嵌套在下面的几个目录中:
/main/tools/planners.php
我的图像位于in:
/images/staticons/
我的代码看起来像:
<td onClick="document.location.href='planners/agility.php';">Contents</a></td>
我希望能够将 $root 目录用于“planners/agility.php”部分,但这样做是这样的:
onClick="document.location.href='<?php echo $root; ?>/planners/agility.php';"
导致 onClick 部分根本不起作用。除此之外,我还想使用相同的方法,使用 $root
变量来定义表格单元格背景。我尝试在background="" 定义中使用PHP 标签,但图像在Chrome 或Firefox 中不显示——仅在IE9 中显示。
I'm using a simple solution for getting the server root and it works quite well with my includes:
<?php $root = $_SERVER['DOCUMENT_ROOT'].'/frizkie'; ?>
I'm doing some tables, and the .php file that contains the table is nested quite a few directories down at:
/main/tools/planners.php
My images are located in:
/images/staticons/
And my code looks like:
<td onClick="document.location.href='planners/agility.php';">Contents</a></td>
I'd like to be able to use the $root directory for the 'planners/agility.php' part, but doing so like this:
onClick="document.location.href='<?php echo $root; ?>/planners/agility.php';"
Causes the onClick part to not work at all. On top of this, I'd also like to define table cell backgrounds using the same method, using the $root
variable. I've tried using PHP tags in the background="" definition, but the images don't show in Chrome or Firefox - only IE9.
发布评论
评论(2)
正如您已经指出的,
$_SERVER['DOCUMENT_ROOT']
用于服务器端路径。您不需要将其放入标记、CSS 和 JS 中。我会坚持使用绝对相对路径,例如/images/background.jpg
(注意前导斜杠)。这样,即使您的页面是通过http://example.com/blog/123-my-beautiful-post/
这样的 URL 访问的,图像也会从
http://example 下载.com/images/background.jpg
而不是
http://example.com/images/blog/123-my-beautiful-post/images/background.jpg
。当包含分散在文档根目录中的多个文件时,使用 DOCUMENT_ROOT 会有很大帮助 - 在这里使用相对路径让我很头疼。不过,如果您确实想在页面中使用完整的网址,则可以尝试使用
$_SERVER['SERVER_NAME']
,但我真的不明白这样做的必要性。$_SERVER['DOCUMENT_ROOT']
is for server-side pathing so to speak, as you already noted. You don't need to put it in your markup, CSS and JS. I'd stick with absolute-relative paths like/images/background.jpg
(note the leading slash). That way even if your page is accessed by a URL likehttp://example.com/blog/123-my-beautiful-post/
the image is downloaded from
http://example.com/images/background.jpg
and not
http://example.com/images/blog/123-my-beautiful-post/images/background.jpg
.Using
DOCUMENT_ROOT
helps a lot when including multiple files scattered around your document root - using relative paths here gives me a headache. If you really want full urls in your pages, though, you can try using$_SERVER['SERVER_NAME']
for that matter, but I really don't understand the need to do so.当您编写 HTML 标记时,请勿在 href 中使用服务器路径。相反,href 应该与您网站域的基础相关。
当您想通过 require/require_once/include/include_once 加载 PHP 文件时,请使用文档根目录。
另外,您的代码不起作用的原因可能是因为在某些 Web 服务器安装上,文档根目录存在以下三个条件之一(在 Apache httpd.conf 中设置):
因此,您必须检查所有三个条件,然后决定始终使用尾部斜杠。
如果未设置,则应将 $docroot 显式设置为已定义的常量,例如 Define("DOCROOT", "/user/home/jayleno/"),然后在整个代码中使用它。
When you are writing HTML markup, don't use server paths in your hrefs. Instead, the hrefs should be relative from the base of your website domain.
Use document root when you want to load PHP files via require/require_once/include/include_once.
Also, the reason why your code didn't work is likely because on some web server installations, one of three conditions exist for document root (set in Apache httpd.conf):
So you have to check for all three conditions and then decide, say, to always have a trailing slash.
If it isn't set, you should set a $docroot explicitly to a defined constant like define("DOCROOT", "/user/home/jayleno/") and then use that throughout your code.