PHP如何找到应用程序的根目录? [英] PHP how to find application root?

查看:174
本文介绍了PHP如何找到应用程序的根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的包含文件有问题.当我使用require_once('somefile.php')时,我似乎无法弄清楚如何构造URL.如果我尝试在目录结构不同的多个地方使用一个包含文件,则会收到一个错误消息,指出找不到该包含文件.

I'm having problems with my include files. I don't seem to be able to figure out how to construct my URLs when I use require_once('somefile.php'). If I try to use an include file in more than one place where the directory structures are different, I get an error that the include file cannot be found.

在asp.net中,要获取我的应用程序的根路径,可以使用〜/directory/file.aspx.斜杠前斜杠始终知道我是从我的网站根目录引用的,并且无论请求来自我网站内的哪个位置,都可以找到该文件.它总是指回根并从那里寻找文件.

In asp.net, to get my application root path, I can use ~/directory/file.aspx. The tild forward slash always knows that I am referencing from my website root and find the file no matter where the request comes from within my website. It always refers back to the root and looks for the file from there.

问题::如何获取网站的根路径?如何做到这一点,以便可以在网站内的任何地方重复使用包含文件?我必须在URL中使用绝对路径吗?

QUESTION: How can I get the root path of my site? How can I do this so I can reuse my include files from anywhere within my site? Do I have to use absolute paths in my URLs?

谢谢!

推荐答案

$_SERVER['DOCUMENT_ROOT']应该具有您的Web服务器的根路径.

There is $_SERVER['DOCUMENT_ROOT'] that should have the root path to your web server.

如果您查看大多数主要的php程序.使用安装程序时,通常输入应用程序文件夹的完整路径.安装程序会将其放入整个应用程序中包含的配置文件中.一种选择是使用自动前置文件来设置变量.另一种选择是仅在需要的每个页面上 include_once().我建议的最后一个选择是使用引导程序编写应用程序,在该程序中,您可以通过文件(通常使用url_rewrite).这样一来,您就可以轻松地在一个位置设置/包含配置变量,并使它们在所有脚本中都可用.

If you look at most major php programs. When using the installer, you usually enter in the full path to the the application folder. The installer will just put that in a config file that is included in the entire application. One option is to use an auto prepend file to set the variable. another option is to just include_once() the config file on every page you need it. Last option I would suggest is to write you application using bootstrapping which is where you funnel all requests through one file (usually with url_rewrite). This allows you to easily set/include config variables in one spot and have them be available throughout all the scripts.

这篇关于PHP如何找到应用程序的根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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