如何包含需要绝对路径的PHP文件? [英] How to include PHP files that require an absolute path?
问题描述
我的目录结构如下:
script.php
inc/include1.php
inc/include2.phpobjects/object1.php
objects/object2.phpsoap/soap.php
现在,我在script.php
和/soap/soap.php
中都使用了这些对象,可以移动它们,但是出于特定的原因,我希望像这样的目录结构.当执行script.php
时,包含路径为inc/include.php
;当执行/soap/soap.php
时,包含路径为../inc
,绝对路径有效,/mnt/webdev/[project name]/inc/include1.php...
,但是如果我想将目录移动到其他位置,这是一个丑陋的解决方案.>
那么有没有办法使用相对路径,或者以编程方式生成"/mnt/webdev/[project name]/"
?
这应该有效
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/inc/include1.php";
编辑:通过审视
I have a directory structure like the following;
script.php
inc/include1.php
inc/include2.phpobjects/object1.php
objects/object2.phpsoap/soap.php
Now, I use those objects in both script.php
and /soap/soap.php
, I could move them, but I want the directory structure like that for a specific reason. When executing script.php
the include path is inc/include.php
and when executing /soap/soap.php
it's ../inc
, absolute paths work, /mnt/webdev/[project name]/inc/include1.php...
But it's an ugly solution if I ever want to move the directory to a different location.
So is there a way to use relative paths, or a way to programmatically generate the "/mnt/webdev/[project name]/"
?
This should work
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/inc/include1.php";
Edit: added imporvement by aussieviking
这篇关于如何包含需要绝对路径的PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!