Setting a root directory for accessing files from subfolders?

时光毁灭记忆、已成空白 提交于 2019-12-14 03:19:34

问题


I am trying to create a root directory on my localhost(using xampp) so that I can link to images and folders found on the root from subfolders. So my directory is built like seen below.

Also, would this method work when the site is uploaded on the web? or would I have to change document root to http:// links to my site?

F = folder f = file

My Site
-------
[f] index.html
[f] logo.html
[F] css -> [f] main.css
[F] news -> [F]1 -> [f] 1.html
[F] images -> [f]logo.jpg

logo.html will be included using PHP on every html page. So if I include the logo.html in both index.html & news/1/1.html link to image doesn't work since the news file is on a different folder and link to the images folder changes.

I tried this using PHP but it seems I am doing it wrong:

<?php $root = $_SERVER['DOCUMENT_ROOT'].'/My Site/'; echo $root .'/images/logo.jpg';
?>

回答1:


$_SERVER['DOCUMENT_ROOT'] gives you the real file-system path, not the path returned by Apache.

To link to the resource you need to use the HTTP path. If you want to link from the root you just need something like /images/logo.jpg




回答2:


I found that on my PHPBB3.1 driven website using the "Pages" addin, the relative link would not remain 'static' in some link buttons at the top of my site.

From the PHPbb3 index page a link such as app.php/page/FT would produce an address of http://earth/forum/app.php/page/FT (Correct) ,but from the "Pages add in) that same link produced http://earth/forum/app.php/page/app.php/page/FT (incorrect).

I started looking for a way to set my page relative to the root of the web. I could NOT use absolute links because my site is replicated on other servers. I found the answer by trial and error. Simply add several ../../../ like this ../../../forum/app.php/page/FT and it works.

Perhaps not the best of computer programming but it does the job. In a nutshell with each ../ you step back a directory, put too many in and it goes back to the root but cannot go back any further!



来源:https://stackoverflow.com/questions/12786588/setting-a-root-directory-for-accessing-files-from-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!