PHP 包含路径

发布于 2024-08-31 06:39:01 字数 503 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林很绿却致人迷途 2024-09-07 06:39:01

我绝对不会使用。我以前遇到过很多这方面的问题。如果您使用 ,您的所有链接都将与该基本值相关。

相反,我建议为公共目录设置 PHP 常量。例如:

PHP 代码:

<?php
    define('CSS_DIR', '/SysProgs/required/CSS/');
?>

HTML 代码:

<link href="<?php echo CSS_DIR ?>style.css" rel="stylesheet" type="text/css" />

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:

<?php
    define('CSS_DIR', '/SysProgs/required/CSS/');
?>

HTML Code:

<link href="<?php echo CSS_DIR ?>style.css" rel="stylesheet" type="text/css" />
寻梦旅人 2024-09-07 06:39:01

一个想法

使用 header.html 中的完整 URL。这将是明确且稳健的。

<head>
<link href="/FULL_BASE_URL/style/style.css" rel="stylesheet" type="text/css" />
</head>

另一个想法

使用 标头标记。这允许您指定链接的基本 URL(包括 CSS),并且短期内可能需要最少的工作(参见下面的注释)。

<head>
<base href="FULL_BASE_URL" />
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

更多信息请参见 w3schools

注意: 正如评论中所述下面的base最终可能会导致比其价值更多的混乱。

One Idea

Use the full URL in header.html. This will be unambiguous and robust.

<head>
<link href="/FULL_BASE_URL/style/style.css" rel="stylesheet" type="text/css" />
</head>

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).

<head>
<base href="FULL_BASE_URL" />
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

More at w3schools

Note: As is noted in the comments below base may ultimately cause more confusion than it is worth.

忱杏 2024-09-07 06:39:01

我喜欢在应用程序的中心位置定义绝对路径和网络根目录:

<?php

  define("APP_WEBROOT", "/myapp"); 
  define("APP_ROOTDIR", "/home/www/example.com/htdocs/myapp");

 ?>

然后您可以“绝对化”正确的链接,如下所示:

<?php echo APP_WEBROOT; ?>/software/CSS/style.css

我更喜欢这个

  • 而不是 因为 该标签会造成混乱,并且使代码从长远来看更难维护

  • 因为该标签会造成混乱,并且从长远来看使代码更难维护

    路径 /software/CSS/style.css 因为这些会使您无法将应用程序安装在您选择的子目录中:您将始终绑定到根目录。

I like to define both an absolute path and a webroot in a central place in your application:

<?php

  define("APP_WEBROOT", "/myapp"); 
  define("APP_ROOTDIR", "/home/www/example.com/htdocs/myapp");

 ?>

you can then "absolutize" the correct links like so:

<?php echo APP_WEBROOT; ?>/software/CSS/style.css

I prefer this

  • over <base> because that tag creates confusion and makes code harder to maintain in the long run

  • over 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.

月下伊人醉 2024-09-07 06:39:01

我在设计网站时经常遇到这个问题。当我有自定义 CMS 时,我使用以下内容:

$basedir = "root_directory/";
$basedirPHP = $_SERVER['DOCUMENT_ROOT'].$basedir;
$basedirHTML = "http://".$_SERVER['SERVER_NAME'].$basedir;

我定义 $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:

$basedir = "root_directory/";
$basedirPHP = $_SERVER['DOCUMENT_ROOT'].$basedir;
$basedirHTML = "http://".$_SERVER['SERVER_NAME'].$basedir;

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.

泪是无色的血 2024-09-07 06:39:01

您首先要了解的是,您的问题与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文