如何在文件调用中引用本地主机或我的域名?

发布于 2024-10-29 01:02:09 字数 617 浏览 1 评论 0原文

我一直在开发一个网站,但我首先在本地计算机上开发。我可以通过浏览器使用 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 技术交流群。

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

发布评论

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

评论(3

以歌曲疗慰 2024-11-05 01:02:09
$_SERVER['DOCUMENT_ROOT']

应该获取服务器根路径,也在“www”文件夹下方

$_SERVER['DOCUMENT_ROOT']

should get the server root path, also below the 'www' folder

请爱~陌生人 2024-11-05 01:02:09

有时使用的解决方案是将网站根目录的绝对 URL 存储到一个变量中,一次:

$root = 'http://www.yoursite.com';

有两种方法可以实现此目的:

  • 如果您将应用程序部署到一个非常不寻常的位置,您可以在配置文件中设置
  • 它,这一般可以从$_SERVER中的一项来确定。

注意:您可能更喜欢使用常量而不是变量。

然后,在所有

<link href="<?php echo $root; ?>/css/style.css";

当然,这意味着您的 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 :

$root = 'http://www.yoursite.com';

Two ways to get this :

  • If you are deploying your application to a quite unusual location, you could set that in your configuration file
  • Else, this can generally be determines from one of the items in $_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 :

<link href="<?php echo $root; ?>/css/style.css";

Of course, this means your style.css must remain in the css folder, always -- but it's probably going to be the case, isn't it ?

鹊巢 2024-11-05 01:02:09

我认为 这篇文章 就是您正在寻找的内容。

I think that this post is what you are looking for.

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