如何包含需要绝对路径的PHP文件? [英] How to include PHP files that require an absolute path?

查看:146
本文介绍了如何包含需要绝对路径的PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录结构如下:

script.php

inc/include1.php
inc/include2.php

objects/object1.php
objects/object2.php

soap/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.php

objects/object1.php
objects/object2.php

soap/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆