根路径不适用于 php include

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1400795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:25:00  来源:igfitidea点击:

root path doesn't work with php include

phppathincluderoot

提问by Paul Dixon

/ in the beginning of a link to get to the root folder doesn't work in php include.

/ 在指向根文件夹的链接的开头在 php include 中不起作用。

for example "/example/example.php"

例如“/example/example.php”

What is the solution?

解决办法是什么?

回答by Paul Dixon

I'm assuming by root folder you mean your web document root, rather than filesystem root.

我假设根文件夹是指您的 Web 文档根,而不是文件系统根。

To that end, you can either

为此,您可以

  • add the web root folder to the include path, and include('example/example.php')
  • or you can include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
  • 将 web 根文件夹添加到包含路径,以及include('example/example.php')
  • 或者你可以 include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')

回答by Ian

I had this issue too. Paul Dixon's answer is correct, but maybe this will help you understand why:

我也有这个问题。保罗迪克森的回答是正确的,但也许这会帮助你理解为什么:

The issue here is that PHP is a server side language. Where pure HTML documents can access files based on the root url you set up on the server (ie. to access an image from any sub-directory you're on you would use /images/example.jpgto go from the top directory down), PHP actually accesses the server root when you use include (/images/example.jpg)

这里的问题是 PHP 是一种服务器端语言。纯 HTML 文档可以根据您在服务器上设置的根 url 访问文件(即从您所在的任何子目录访问图像,您将使用/images/example.jpg从顶级目录向下访问),PHP 实际上访问使用时的服务器根目录include (/images/example.jpg)

The site structure that you have set up actually lies within a file system in the Apache Server. My site root looks something like this, starting from the server root and going down:

您设置的站点结构实际上位于 Apache 服务器的文件系统中。我的站点根目录看起来像这样,从服务器根目录开始向下:

/home2/siteuserftp/public_html/test/

"test" represents your site root

“test”代表您的站点根目录

So to answer your question why your PHP include isn't getting the result you want (it is working exactly as it should) is because you're asking the PHP code to try and find your file at the server root, when it is actually located at the HTML root of your site which would look something like the above.

所以要回答你的问题,为什么你的 PHP 包含没有得到你想要的结果(它完全按照它应该的方式工作)是因为你要求 PHP 代码尝试在服务器根目录中找到你的文件,而实际上它是位于您网站的 HTML 根目录中,类似于上面的内容。

Your file would be based on the site root of "test/" and would look something like this:

您的文件将基于“test/”的站点根目录,如下所示:

/home2/siteuserftp/public_html/test/about/index.php

/home2/siteuserftp/public_html/test/about/index.php

The answer Paul Dixon provided:

Paul Dixon 给出的答案是:

include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')

is exactly what will fix your problem (don't worry about trying to find the document root to replace 'DOCUMENT_ROOT', PHP will do it for you. Just make sure you have 'DOCUMENT_ROOT' literally in there)

正是解决您的问题的方法(不要担心试图找到文档根目录来替换“DOCUMENT_ROOT”,PHP 会为您完成。只要确保您在其中包含“DOCUMENT_ROOT”即可)

EDIT:

编辑:

More information DOCUMENT_ROOT and other PHP SERVER variables can be found here

可以在此处找到更多信息 DOCUMENT_ROOT 和其他 PHP SERVER 变量

回答by Peter Bailey

include() (and many other functions like require(), fopen(), etc) all work off the local filesystem, not the web root.

include()(以及许多其他函数,如 require()、fopen() 等)都在本地文件系统上工作,而不是在 web 根目录下。

So, when you do something like this

所以,当你做这样的事情时

include( "/example/example.php" );

You're trying to include from the root of your *nix machine.

您正在尝试从 *nix 机器的根目录中包含。

And while there are a multitude of ways to approach what you're doing, Paul Dixon's suggestions are probably your best bets.

虽然有多种方法可以处理您正在做的事情,但 Paul Dixon 的建议可能是您最好的选择。

回答by Gameselop

Every web server has a public_htmlfolder, in which you usually keep your files etc. By using /, you will not get to public_html, instead you direct towards the main (unaccesible) root. So, use $_SERVER['DOCUMENT_ROOT']."/your/locati.on"instead

每个 Web 服务器都有一个public_html文件夹,您通常将文件等保存在其中。通过使用/,您将无法访问public_html,而是直接指向主(无法访问的)根目录。所以,$_SERVER['DOCUMENT_ROOT']."/your/locati.on"改用

回答by David

I solved this on a machine running Windows and IIS with the following:

我在运行 Windows 和 IIS 的机器上解决了这个问题:

<?php
  $docroot = 'http://www.example.com/';
  include ($docroot.'inc-header.php');
?>

If you're on a local dev machine, you can force your domain to point to localhost by adding the following in C:\Windows\System32\drivers\etc\hosts

如果您在本地开发机器上,您可以通过在 C:\Windows\System32\drivers\etc\hosts 中添加以下内容来强制您的域指向 localhost

127.0.0.1   www.example.com

Also, you'll need to enable allow_url_include in php.ini like so

此外,您需要像这样在 php.ini 中启用 allow_url_include

allow_url_include = On

回答by Andrey Baluevsky

For me, the following trick worked. I'm using Windows with IIS, so DOCROOT is C:\Inetpub\wwwroot.

对我来说,以下技巧奏效了。我使用带有 IIS 的 Windows,所以 DOCROOT 是 C:\Inetpub\wwwroot。

  1. do subst of C:\Inetpub\wwwroot to a drive. Let it be W: (WEB contents).
  1. 对驱动器执行 C:\Inetpub\wwwroot 的 subst。让它成为W:(WEB内容)。
    subst W: C:\Inetpub\wwwroot
  1. edit php.ini this way: append W:\ to include_path, change doc_root to W:\
  1. 以这种方式编辑 php.ini:将 W:\ 附加到 include_path,将 doc_root 更改为 W:\
include_path = ".;c:\php\active\includes;W:\"
doc_root = W:\

  1. put subst command into CMD file within Startup folder to make mapping automatically.
  1. 将 subst 命令放入 Startup 文件夹中的 CMD 文件中以自动进行映射。

Now, both versions allowed:

现在,两个版本都允许:

    include '/common/common.inc'; // access to mapped W: root
    include 'common/common.inc'; // access to W: within include_path

回答by user1123382

some versions of PHP may have the delimiter at the end of document root while others may not. As a practical matter you may want to use:

某些版本的 PHP 可能在文档根目录的末尾有分隔符,而其他版本可能没有。作为一个实际问题,您可能想要使用:

    $r = trim(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING));
    if (substr($r, 0, 1) == '/')
    {
      define("PATCH_SEPARATOR", "/");  
    }
    else
    {
      define("PATCH_SEPARATOR", "\");    
    }

    if (substr($r, -1) == PATCH_SEPARATOR)
    {
      include_once ($r . 'example/example.php');
    }
    else
    {
      include_once ($r . PATCH_SEPARATOR . 'example/example.php');
    }

回答by user2380825

maybe it's a bit unconventional

也许这有点不合常规

If I have a case like

如果我有这样的案例

/var/www/namedir/  <= root

/var/www/namedir/example/example.php <= file to include

-- directory when i need the include -- 
/var/www/namedir/dir1/page.php 
/var/www/namedir/dir1/dirA/page.php
/var/www/namedir/dir1/dirB/page.php

the solution that I use is simple. get the path before the "Dir1"

我使用的解决方案很简单。获取“Dir1”之前的路径

something like this

像这样的东西

include (substr(dirname(__FILE__),0,strpos(dirname(__FILE__), '/dir1'))."/example/example.php");

I found it usefull id i need to rename the main subdir for example from

我发现它很有用,我需要重命名主子目录,例如从

/var/www/namesite/internalDirA/dirToInclude/example.php  
/var/www/namesite/internalDirA/dir1/dirA/example.php 
/var/www/namesite/internalDirA/dir1/dirB/example.php 

TO

/var/www/namesite/dirReserved/dirToInclude/example.php  
/var/www/namesite/dirReserved/dir1/dirA/example.php 
/var/www/namesite/dirReserved/dir1/dirB/example.php