且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置从子文件夹访问文件的根目录?

更新时间:2023-12-05 13:47:46

$ _ SERVER ['DOCUMENT_ROOT '] 为您提供真正的文件系统路径,而不是Apache返回的路径。



要链接到资源,您需要使用HTTP路径。如果你想从根连接你只需要像 /images/logo.jpg


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';
?>

$_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