PHP 包含路径
我是 PHP 新手,在尝试使用 include 链接 CSS 文件时遇到问题。
基本上我需要我的文件链接回某个目录,无论文件位于多远的位置。我尝试过使用
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/sysprogs/required/header.html';
?>
But header.html 包含指向我的 css 文件的链接,因此它最终在其中查找 css 文件的目录
http://localhost/SysProgs/software/CSS/style.css
而不是我希望它去的位置,
http://localhost/SysProgs/required/CSS/style.css
我希望这是有意义的,我希望你能提供帮助我
感谢大家的帮助!
I'm new to PHP and I'm having a problem when trying to link my CSS files using include.
Basically I need my files to link back to a certain directory no matter how far down the file is. I have tried using
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/sysprogs/required/header.html';
?>
But header.html contains the links to my css files so the directory it ends up looking for the css files in is
http://localhost/SysProgs/software/CSS/style.css
instead of where I want it to go to which is
http://localhost/SysProgs/required/CSS/style.css
I hope that made sense and I hope you can help me
Thankyou for all your help everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我绝对不会使用
。我以前遇到过很多这方面的问题。如果您使用
,您的所有链接都将与该基本值相关。相反,我建议为公共目录设置 PHP 常量。例如:
PHP 代码:
HTML 代码:
I would definitely not use
<base>
. I've run into many problems with this before. If you use<base>
, ALL of your links will become relative to that base value.Instead, I would recommend setting PHP constants for common directories. For example:
PHP Code:
HTML Code:
一个想法
使用
header.html
中的完整 URL。这将是明确且稳健的。另一个想法
使用
标头标记。这允许您指定链接的基本 URL(包括 CSS),并且短期内可能需要最少的工作(参见下面的注释)。更多信息请参见 w3schools
注意: 正如评论中所述下面的
base
最终可能会导致比其价值更多的混乱。One Idea
Use the full URL in
header.html
. This will be unambiguous and robust.Another Idea
Use the
<base>
header tag. This allows you to specify a base URL for links, including CSS, and may require the least work in the short term (see note below).More at w3schools
Note: As is noted in the comments below
base
may ultimately cause more confusion than it is worth.我喜欢在应用程序的中心位置定义绝对路径和网络根目录:
然后您可以“绝对化”正确的链接,如下所示:
我更喜欢这个
而不是
因为 该标签会造成混乱,并且使代码从长远来看更难维护,
路径
/software/CSS/style.css
因为这些会使您无法将应用程序安装在您选择的子目录中:您将始终绑定到根目录。I like to define both an absolute path and a webroot in a central place in your application:
you can then "absolutize" the correct links like so:
I prefer this
over
<base>
because that tag creates confusion and makes code harder to maintain in the long runover using absolute paths
/software/CSS/style.css
because those make you unable to install your application in a subdirectory of your choice: You will always be bound to the root directory.我在设计网站时经常遇到这个问题。当我有自定义 CMS 时,我使用以下内容:
我定义 $basedir,以便我可以毫不费力地将站点移动到服务器中的不同子目录。 $basedirPHP 和 $basedirHTML 是这样我可以使用 php 调用文件,或者像你提到的那样,在链接 CSS、JS 等时。
如果你使用的是 WordPress,只需使用好的 ol'
bloginfo('template_directory ');
在模板文件中执行相同的操作。I run into this problem a lot when designing sites. When I have custom CMS, I use the following:
I define $basedir so I can move the site to different subdirectories in the server without any effort. The $basedirPHP and $basedirHTML are so I can call on files either with php, or like you mentioned, when linking CSS, JS, etc.
If you're on wordpress, just use the good ol'
bloginfo('template_directory');
to do the same in template files.您首先要了解的是,您的问题与 PHP 无关。它就像 HTML 问题中的文件名一样简单。如果没有 PHP,它将保持不变。只需使用 CSS 文件的绝对路径
还有另一件事要考虑:考虑接受一些问题。
The first thing for you to understand, is your question has nothing PHP related. It is as simple as just filenames in your HTML questuon. Without PHP it will remain the same. Just use absolute path to your CSS file
And another thing to think of: consider to accept some questions.