简体   繁体   English

PHP包含路径

[英]PHP Include Paths

I'm new to PHP and I'm having a problem when trying to link my CSS files using include. 我是PHP新手,尝试使用include链接CSS文件时遇到问题。

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 但是header.html包含指向我的css文件的链接,因此最终在其中查找css文件的目录是

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! 谢谢大家的帮助!

I would definitely not use <base> . 我绝对不会使用<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. 如果使用<base> ,则所有链接都将相对于该基本值。

Instead, I would recommend setting PHP constants for common directories. 相反,我建议为公共目录设置PHP常量。 For example: 例如:

PHP Code: PHP代码:

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

HTML Code: HTML代码:

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

One Idea 一个想法

Use the full URL in header.html . 使用header.html的完整URL。 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. 使用<base>标头标记。 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 ). 这使您可以为链接(包括CSS)指定基本URL,并且在短期内可能需要最少的工作( 请参阅下面的注释 )。

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

More at w3schools w3schools的更多内容

Note: As is noted in the comments below base may ultimately cause more confusion than it is worth. 注意:如下面的评论所述, base可能最终导致混乱,其价值超过其价值。

I like to define both an absolute path and a webroot in a central place in your application: 我想在应用程序的中央位置定义绝对路径和webroot:

<?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 <base>之上,因为该标签会造成混乱,并且从长远来看使代码难以维护

  • 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. 不使用绝对路径/software/CSS/style.css因为它们使您无法将应用程序安装在您选择的子目录中:您将始终绑定到根目录。

I run into this problem a lot when designing sites. 设计网站时,我经常遇到这个问题。 When I have custom CMS, I use the following: 使用自定义CMS时,请使用以下内容:

$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. 我定义了$ basedir,因此我可以毫不费力地将站点移至服务器中的其他子目录。 The $basedirPHP and $basedirHTML are so I can call on files either with php, or like you mentioned, when linking CSS, JS, etc. 使用$ basedirPHP和$ basedirHTML可以在链接CSS,JS等时使用php或您提到的文件来调用文件。

If you're on wordpress, just use the good ol' bloginfo('template_directory'); 如果您使用的是wordpress,只需使用优质的' bloginfo('template_directory'); to do the same in template files. 在模板文件中执行相同的操作。

The first thing for you to understand, is your question has nothing PHP related. 您首先要了解的是,您的问题与PHP没有任何关系。 It is as simple as just filenames in your HTML questuon. 它就像HTML问题中的文件名一样简单。 Without PHP it will remain the same. 没有PHP,它将保持不变。 Just use absolute path to your CSS file 只需使用CSS文件的绝对路径

And another thing to think of: consider to accept some questions. 还有另一件事要考虑:考虑接受一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM