php 如何对包含的文件(如 .css)使用相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12432913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use relative paths for included files such as .css
提问by zhang ruiting
I have a header.phpfile which contains a .cssfile link.
我有一个header.php包含.css文件链接的文件。
When I "include" header.phpinto another php file in different folder, the .csshref for that header.phpis not correct for the new php file.
当我“包含”header.php到不同文件夹中的另一个 php 文件时,新的 php 文件的.csshrefheader.php不正确。
How should I declare the hrefin my .cssfile to include the header.phpwith it that will be correct for any folder that php file is in?
我应该如何href在我的.css文件中声明以包含对header.phpphp 文件所在的任何文件夹都是正确的?
回答by discomatt
This is a reason many large applications will try to set a 'root URI' constant/variable when installing.
这是许多大型应用程序在安装时会尝试设置“根 URI”常量/变量的原因。
While /css/style.csswill work if your application is located in the root directory of the domain/subdomain, it will fail if it isn't (/appName/css/style.css)
/css/style.css如果您的应用程序位于域/子域的根目录中,虽然可以工作,但如果不是 ( /appName/css/style.css)
Store the absolute URI to the 'root' script folder along with other configuration constants/variables, and building absolute links becomes a breeze.
将绝对 URI 与其他配置常量/变量一起存储到“根”脚本文件夹,构建绝对链接变得轻而易举。
define( 'SCRIPT_ROOT', 'http://localhost/yourApplication' );
// ...
echo '<link rel="stylesheet" type="text/css" href="'.SCRIPT_ROOT.'/css/style.css">';
回答by Tom
You have few options, which i've tried to gather here
你有几个选择,我试图在这里收集
base href
基本href
<head>
<base href="http://www.mysite.com/" />
</head>
What it does, is sets all hrefs to point to certain path.
With this set, you can use <link rel='stylesheet' href='css/mycss.css' />and successfully load mycss.css file even if you're page is deep down in http://www.mysite.com/pages/2012/public/secret_folder/myownphpfile.php
它的作用是将所有 href 设置为指向某个路径。使用此设置,<link rel='stylesheet' href='css/mycss.css' />即使您的页面位于深处,您也可以使用并成功加载 mycss.css 文件http://www.mysite.com/pages/2012/public/secret_folder/myownphpfile.php
absolute paths
绝对路径
You can always use aboslute paths such as, but it can be a pain to change folders of files later on.
您始终可以使用绝对路径,但稍后更改文件文件夹可能会很痛苦。
<link rel='stylesheet' href='http://www.mysite.com/css/mycss.css' />
defined pats
定义拍片
As @Discomatt told, using PHP defined paths is a easy way to keep things working. Downside; you have to use PHP. if you use it anyways, no problems ^^
正如@Discomatt 所说,使用 PHP 定义的路径是保持工作正常的一种简单方法。缺点;你必须使用PHP。如果你无论如何使用它,没有问题^^
define('CSSDIR', 'http://www.mysite.com/css/);
<link rel='stylesheet' href='<?= CSSDIR ?>mycss.css' />
回答by user1873439
I can see one reason for wanting to have dynamic and relative path generation for href links, and that is if you run your project on multiple domains or sites that have different paths. (For example, your project is available on http://myproject.example.org/and also on http://example.org/myprojecttest/). If this is not the case, I would suggest directly specifying your css includes relative to the root folder:
我可以看到希望为 href 链接生成动态和相对路径的一个原因,那就是如果您在具有不同路径的多个域或站点上运行您的项目。(例如,您的项目可在http://myproject.example.org/和http://example.org/myprojecttest/ 上找到)。如果不是这种情况,我建议直接指定相对于根文件夹的 css 包含:
<link href="/css/style.css" />
If this does apply to you, try this:
如果这确实适用于您,请尝试以下操作:
In every top-level documentthat requires header.php, add a $ROOT variable that indicates the top-level document's location compared to the root. e.g.:
在每个需要 header.php 的顶级文档中,添加一个 $ROOT 变量,该变量指示与根目录相比的顶级文档的位置。例如:
$ROOT = './';
or
或者
$ROOT = '../';
or
或者
$ROOT = '../../';
Now, in your header.php file, you can use:
现在,在您的 header.php 文件中,您可以使用:
<link href="<?php echo $ROOT; ?>css/style.css" />
This allows you to make a header.php file that will work for any page at any relative path.
这允许您创建一个 header.php 文件,该文件适用于任何相对路径下的任何页面。
Full Example
完整示例
Included File (/path/header.php)
包含文件 (/path/header.php)
<html><body>
<head>
<link href="<?php echo $ROOT; ?>css/style.css" />
[...]
File 1 (/path/index.php):
文件 1 (/path/index.php):
<?php
$ROOT = './';
include 'header.php';
?>
File 1 (/path/admin/index.php):
文件 1 (/path/admin/index.php):
<?php
$ROOT = '../';
include '../header.php';
?>
File 3 (/path/admin/test/magic.php):
文件 3 (/path/admin/test/magic.php):
<?php
$ROOT = '../../';
include '../../header.php';
?>
回答by Munteanu Sergiu
Write the absolute path of the css file like:
写入css文件的绝对路径,如:
<link href="http://site.com/css/style.css" />
<link href="http://site.com/css/style.css" />
回答by Paul Dessert
Like this:
像这样:
<link rel="stylesheet" href="/css/style.css" />
The first /says "Go to the root directory and look from there". It's a relative path.
第一个/说“转到根目录并从那里查看”。这是一条相对路径。
回答by Deplicator
Based on Tom's answerI did a combination of base urland a PHP define(for less typing).
根据汤姆的回答,我做了base url一个 PHP的组合define(为了减少打字)。
Somewhere in PHP:
PHP 中的某个地方:
define("HOST_BASE", "http://example.com/");
Then in the header file:
然后在头文件中:
<base href="<?php echo HOST_BASE ?>">
The base tag was new to me and I was worried about compatibility, but it appears to work well. There is more info about it at MDN.
回答by Ravi
Simply defineyour base URL and concatenate it in CSS or JavaScript reference.
只需定义您的基本 URL 并将其连接到 CSS 或 JavaScript 参考中。
Define it as below.
定义如下。
define ('host_address','http://localhost:85/grc/');
define('css', host_address.'styles/');
I have defined base URL by host_addressand CSSand used both of them as below.
我已经通过host_address和CSS定义了基本 URL,并使用了它们,如下所示。
<link rel="stylesheet" href='.css.'custom.css>
回答by Tobias Buschor
Have a look at this answer to load css relative to the current js-file:
https://stackoverflow.com/a/56580810/4865307
看看这个答案来加载相对于当前 js 文件的 css:https:
//stackoverflow.com/a/56580810/4865307
With this trick, you just call c1CssImport() where you like to include relative-url based css files:
使用这个技巧,你只需调用 c1CssImport() 你喜欢包含基于相对 url 的 css 文件:
c1CssImport('./ui.css');

