所以目前我是一名网络开发新手。我对网站“应该”如何格式化没有太多经验。尽管我正在尽我最大的努力使一切尽可能井井有条,并且在我编写任何大型程序之前(即:一个包含许多页面的网站,将来可能需要更新。)。
所以我决定学习 php,这样我就可以使网站更易于更新,并且可以处理大量元素。所以,我从简单开始,看看是否可以将所有 css 放在一个位置,以便任何页面都可以轻松加载。加载这些级联表我在 PHP 中创建了一个函数来查找它们的目录,然后加载实际的 php.ini 文件。但是当我从直接路径加载 css 时,它似乎并没有真正加载。它实际上似乎是在尝试从当前目录加载为我拥有的完整路径的相对路径。因此,“文件 url 被禁止”之类的问题。
我试过手动插入 .css 的路径,并使用我的 php 代码——但都不起作用。
我的 php 代码是这样的
<?php // This gives us the current path needed.
$direct = __DIR__;
$dirList = explode( "\\", $direct );
$length = count( $dirList );
$curPath = "";
$curPart = "";
for( $i = 0; $i < $length; $i ++ )
{
$curPart = $dirList[ $i ];
$curPath .= $curPart . "/";
if ( $curPart == "mSite" )
break;
}
$curPath = str_replace("\"", "", $curPath );
?>
我的链接器看起来像这样
<link rel="stylesheet" type="text/css" href="/<?php echo ($curPath."css_sheets/navBar_sheet.css"); ?>" />
<link rel="stylesheet" type="text/css" href="/C:/Users/UserName/Desktop/unServ/New Testing/css_sheets"/>
是的,我的代码可能看起来肯定不好,但我才刚刚起步。任何帮助表示赞赏!我在整个网络和这个网站上都看过了,但我就是想不出如何正确地表达它——或者之前似乎没有人真正遇到过这个问题。
Ps:我试过使用“SRC”而不是“HREF”,我试过将“/”放在路径前,但都没有用。
最佳答案
这篇来自 css-tricks.com 的文章条目完美地回答了我关于相对路径的问题。
Here is all you need to know about relative file paths:
Starting with "/" returns to the root directory and starts there Starting with "../" moves one directory backwards and starts there Starting with "../../" moves two directories backwards and starts there (and so on...) To move forward, just start with the first subdirectory and keep moving forward
转自文章: http://css-tricks.com/quick-reminder-about-file-paths/
关于php - css 链接中的 href 不会从以前的目录加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728240/