(初步说明:这个问题似乎已经有很多变体,但它们似乎都关注物理 PHP 文件本身的文件系统位置,而不是事物的Web URL 位置,这就是我所追求的。)
介绍 1:
我有一个具有以下物理文件系统结构的小型“网站”:
variable.php
folder
test.php
(换句话说,variable.php
在顶层和test.php
文件夹中folder
。)
以下是文件内容:
<?php
//variable.php
$basePath = dirname($_SERVER['SCRIPT_NAME']);
?>
<?php
//test.php
include("../variables.php");
echo $basePath;
?>
介绍 2:
在本地开发这个“网站”时,它在这个地址上:
http://localhost/~noob/test-website
问题:
问题是,如果我放入http://localhost/~noob/test-website/folder/test.php
浏览器,它会打印出/~noob/test-website/folder/
.
I want it to print out /~noob/test-website
(the "web location" of variable.php). In other words, I want to have global access from all files, even ones deeper in the filesystem hierarchy, to /~noob/test-website
. How do I accomplish this?
I obviously do not want to hardcode this path in. The reason is that when I upload it onto a production server, the location changes to something more sane like http://example.com/
(and I don't want to have to modify this hardcoded path after uploading to the server, hence this question of course).