查找回文_查找PHP文档根

查找回文

The PHP document root is the folder where a PHP script is running. When installing a script, web developers often need to know the document root. Although many pages scripted with PHP run on an Apache server, some run under Microsoft IIS on Windows. Apache includes an environment variable called DOCUMENT_ROOT, but IIS doesn't. As a result, there are two methods for locating the PHP document root.

PHP文档根是运行PHP脚本的文件夹。 安装脚本时,Web开发人员通常需要知道文档的根目录。 尽管使用PHP编写脚本的许多页面都在Apache服务器上运行,但某些页面却在Windows上的Microsoft IIS下运行。 Apache包含一个称为DOCUMENT_ROOT的环境变量,而IIS没有。 结果,有两种方法可以定位PHP文档的根目录。

在Apache下查找PHP文档根 ( Finding the PHP Document Root Under Apache )

Instead of emailing tech support for the document root and waiting for someone to respond, you can use a simple PHP script with getenv (), which provides a shortcut on Apache servers to the document root.

可以通过电子邮件使用简单PHP脚本getenv() ,而不是通过电子邮件发送对文档根目录的技术支持并等待他人响应,而可以使用简单PHP脚本,该脚本在Apache服务器上提供了到文档根目录的快捷方式。

These few lines of code return the document root.

这几行代码返回文档的根目录。

在IIS下查找PHP文档根 ( Finding the PHP Document Root Under IIS )

Microsoft's Internet Information Services was introduced with Windows NT 3.5.1 and has been included in most Windows releases since then—including Windows Server 2016 and Windows 10. It does not supply a shortcut to the document root.

Microsoft的Internet Information Services随Windows NT 3.5.1一起引入,此后已包含在大多数Windows版本中,包括Windows Server 2016和Windows10。它不提供文档根目录的快捷方式。

To find the name of the currently executing script in IIS, begin with this code:

要在IIS中找到当前正在执行的脚本的名称,请从以下代码开始:

print getenv ("SCRIPT_NAME");

which returns a result similar to:

返回的结果类似于:

/product/description/index.php

which is the full path of the script. You don't want the full path, just the name of the file for SCRIPT_NAME. To get it, use:

这是脚本的完整路径。 您不需要完整的路径,而只需要SCRIPT_NAME的文件名。 要获得它,请使用:

print realpath(basename(getenv("SCRIPT_NAME")));

which returns a result in this format:

它以以下格式返回结果:

/usr/local/apache/share/htdocs/product/description/index.php

To remove the code referring to the site-relative file and arrive at the document root, use the following code at the beginning of any script that needs to know the document root.

要删除引用站点相对文件的代码并到达文档根目录,请在需要知道文档根目录的任何脚本的开头使用以下代码。

$localpath=getenv("SCRIPT_NAME");
$absolutepath=realpath($localPath);
// fix the Windows slashes
$absolutepath=str_replace("\\","/",$absolutepath);
$docroot=substr($absolutepath,0,strpos($absolutepath,
$localpath));
// an example of use
include($docroot."/includes/config.php");

This method, although more complex, runs on both IIS and Apache servers.

尽管更复杂,但此方法可同时在IIS和Apache服务器上运行。

翻译自: https://www.thoughtco.com/finding-the-document-root-2693942

查找回文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值