如何在文件调用中引用本地主机或我的域名?
我一直在开发一个网站,但我首先在本地计算机上开发。我可以通过浏览器使用 http://localhost/whatever 访问此文件,实际文件位于 /var/www/ 下。然而,我的网络服务器上的文件系统的结构不同;我的文件位于主文件夹中。
我也开始重组,将模块放在单独的文件夹中,而不是将所有内容都放在一个目录中。因此,我正在寻找一种在每种情况下引用我的“基本”目录的方法。考虑到结构将来可能会发生变化,我想要一种引用文件的方法,而不必在上传到我的实时站点后更改这些调用。
例如。
如果我从 includes/header.php
调用它,并且然后我从多个不同的文件位置调用 includes/header.php
,它告诉我它找不到 style.css
,原因很明显: style.css 。 css
不在这些文件夹中!
换句话说,目标是在具有不同目录结构的两个位置之间进行某种可管理的相对路径。
I've been developing a website, but I develop on my local machine first. I can access this with http://localhost/whatever through my browser, and the actual files are under /var/www/. However, the file system on my web server is structured differently; my files are in the home folder.
I am also beginning to restructure such that I am putting modules in separate folders instead of everything being stuck in one directory. I am therefore looking for a way to refer to my 'base' directory in each case. Given that the structure might change in the future, I want a way to refer to files without having to change those calls after uploading to my live site.
Eg. <link rel="stylesheet" type="text/css" href="style.css">
If I call this from includes/header.php
, and then I call includes/header.php
from a number of different file locations, it tells me that it does not find style.css
for obvious reasons: style.css
is not in those folders!
In other words, the goal is some sort of relative pathing that is manageable between two places with different directory structures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该获取服务器根路径,也在“www”文件夹下方
should get the server root path, also below the 'www' folder
有时使用的解决方案是将网站根目录的绝对 URL 存储到一个变量中,一次:
有两种方法可以实现此目的:
$_SERVER
中的一项来确定。注意:您可能更喜欢使用常量而不是变量。
然后,在所有
、
、... 标记中,使用该变量来获得正确的绝对路径:
当然,这意味着您的
style.css
必须始终保留在css
文件夹中 - 但情况可能会如此,不是吗?A solution that's sometimes used is to store the absolue URL of the root of your website to a variable, once :
Two ways to get this :
$_SERVER
.Note: Instead of a variable, you might prefer using a constant.
And, then, in all
<link>
,<script>
, ... tags, use that variable to have a correct absolute path :Of course, this means your
style.css
must remain in thecss
folder, always -- but it's probably going to be the case, isn't it ?我认为 这篇文章 就是您正在寻找的内容。
I think that this post is what you are looking for.