尝试链接到家用电脑上的根目录 [英] Attempting to link to root directory on home computer
问题描述
../
根据需要多次上传1个目录,但是我需要一种方法来链接到我的根目录,而不使用整个地址,所以我可以使用我的PHP页面中的绝对链接,我想使用我的网站,以方便编辑。我被告知, /
将带我回到根目录,但它不在我的电脑上。 这是我最深的文件的地址(在我的第三个目录中)
http://localhost/_Website%20Files/cats/articles/_Blank-Cats2.php
我想要的文件是在中间的 _Website文件。但是这是一个文件夹INSIDE localhost,我相信它被用作根。 Localhost中有一个名为 test.php
的文件,我试图链接以测试LocalHost是否被用作根目录,但它没有显示在_Blank- Cats2.php我试过了(这样做有道理吗?)在_Blank-Cats2.php的某个地方应该有一句话说Php test for website,没有出现。
此外,我使用MAMP作为我的php localhost。如果这有助于任何人。
我做错了,还是试图链接到我的电脑上的根目录的问题?
------编辑------
我已将根目录更改为_Website文件,现在我/我的一些链接正在工作,但其他的不是。
例如,在我 Social-Media-Menu.php
文件中,我包含在我的 _Blank-Cats2.php
页面,使用 /images/facebook.png
找到图像。但是,我不能使用 /Social-Media-Menu.php
找到并包含php文件,我必须使用 ../../ Social-Media-Menu.php
或它将无法正常工作。此外,我可以从保存 _Blank-Cats2.php
中的 /styles.css
链接到我的样式页面页面,但它不会与我的php页面一起工作。我也可以使 /images/logo.gif
工作。
所以基本上我可以使用 /
链接到我的 / images
目录,但根目录本身没有任何东西。为此,我需要使用 ../
或 ../../
也许我只需创建一个新的
------编辑-------
所以我创建了一个新目录,名为 PHP包含
,我把所有需要的文件放在其他地方。 header.php
, menu.php
等等,但是当我去使用 / PHP --includes / header.php
,它没有工作。
但是我注意到,工作的元素是 html / css
。我的 /styles.css
正在工作,我的 /images/logo.gif
正在工作。唯一不能正常工作的东西是我想用PHP加入的东西,所以我认为它与PHP的工作有关。可以这样吗?
-----编辑-------
Aha! /
将不能使用PHP,我发现在这里 https://css-tricks.com/php-include-from-root/ 所以我想我将需要混淆我的PHP代码,以使其行为和做我的想要做如果你有任何提示或技巧如何做到这一点(在我发布的页面上列出的示例对我来说没有意义)请随时发布以下=)
相对路径对您的PHP文件将无法正常工作。我在这里给你的解决方案...
...仅适用于客户端资源(HTML / CSS /图像)。如果要在PHP代码中获取根目录,您必须使用 $ _ SERVER [DOCUMENT_ROOT]
:
然后我们将使用这样的东西,我们的所有包括:
<?php include($ _ SERVER [DOCUMENT_ROOT ]。/your/path.php); ?>
您必须了解一些资源在服务器端(PHP Server)中加载,客户端(浏览器)。这就是为什么你不能为双方工作的通用解决方案。
I'm attempting to build a website on my computer and I'm having issues linking with my root directory from anywhere on the website. I know I can use ../
to go up 1 directory as many times as needed, but I need a way to link to my root directory without using the whole address so I can use "absolute links" to use in my PHP pages which I want to use all over my site for ease of editing. I've been told that /
will take me back to the root, but it isn't working on my computer.
Here is the address for my deepest file (in my 3rd directory from the root) http://localhost/_Website%20Files/cats/articles/_Blank-Cats2.php
The file I want is in _Website Files in the middle.But that's a folder INSIDE localhost, which I believe is being used as the root. There is a file within Localhost called test.php
that I tried to link to to test whether LocalHost was being used as the root, but it didn't show up in _Blank-Cats2.php where I tried it. (does that make sense?) Somewhere in _Blank-Cats2.php there should have been a sentence that said "Php test for website", and it didn't show up.
Also, I'm using MAMP as my php localhost. If that helps anyone in anyway.
Am I doing something wrong, or is the problem with trying to link to a root directory on my computer?
------Edit------ I've changed my root directory to _Website Files, and now some of my /links are working, but others aren't.
For example, in my Social-Media-Menu.php
file which I've included in my _Blank-Cats2.php
page, using /images/facebook.png
worked to locate the image. However, I cannot use /Social-Media-Menu.php
to locate and include the php file, I have to use ../../Social-Media-Menu.php
or it won't work. Also, I was able to link to my style page with /styles.css
from the save _Blank-Cats2.php
page, but it wont work with my php pages. I was also able to make /images/logo.gif
work.
So basically I can use /
to link to my /images
directory, but not anything IN the root directory itself. For that I need to use ../
or ../../
Maybe I'll just create a new directory just for php includes and see if that solves anything.
------Edit-------
So I created a new directory called PHP-includes
and I put all the files I want included elsewhere in there. header.php
, menu.php
, etc. But when I went to use /PHP-includes/header.php
, it didn't work.
However I HAVE noticed that the elements that ARE working are html/css
based. My /styles.css
is working, my /images/logo.gif
is working. The only things NOT working are things I'm trying to include with PHP, so I think it has something to do with how PHP works. Could that be the case?
-----Edit-------
Aha! /
will NOT work with PHP, I found that out here https://css-tricks.com/php-include-from-root/ So I guess I will need to mess around with my PHP code in order for it to behave and do what I want it to do. If you have any tips or tricks on how to do that (the example listed on that page I posted didn't makes sense to me) please feel free to post below =)
The relative paths will not work well with your PHP files. The solution I gave you here...
How do I link to a file in a different directory without using the whole address?
... was meant to work for client side resources only (HTML/CSS/Images). If you want to get the root directory in your PHP code you have to use $_SERVER["DOCUMENT_ROOT"]
:
Then we would use something like this for all our includes:
<?php include($_SERVER["DOCUMENT_ROOT"] . "/your/path.php"); ?>
You have to understand that some resources are loaded in server side (PHP Server) and what is loaded on client side (browser). That's why you can't do a generic solution working for both sides.
这篇关于尝试链接到家用电脑上的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!