根路径不适用于 php include

发布于 2024-08-04 05:30:01 字数 90 浏览 7 评论 0原文

/ 在链接开头获取根文件夹在 php include 中不起作用。

例如“/example/example.php”

解决方案是什么?

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

for example "/example/example.php"

What is the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

罪#恶を代价 2024-08-11 05:30:01

我假设根文件夹是指您的网络文档根目录,而不是文件系统根目录。

为此,您可以

  • 将 Web 根文件夹添加到 包含路径,以及 include('example/example.php')
  • 或者您可以 include($_SERVER['DOCUMENT_ROOT'].'/example/example.php ')

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

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')
亢潮 2024-08-11 05:30:01

我也有这个问题。 Paul Dixon 的答案是正确的,但这也许可以帮助您理解原因:

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

您设置的站点结构实际上位于阿帕奇服务器。我的站点根目录看起来像这样,从服务器根目录开始并向下:

/home2/siteuserftp/public_html/test/

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

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

您的文件将基于“test/”的站点根目录,看起来像这样:

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

Paul Dixon 提供的答案:

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

正是解决您问题的方法(不要不必担心尝试找到文档根目录来替换“DOCUMENT_ROOT”,PHP 会为您完成此操作,只需确保其中确实有“DOCUMENT_ROOT”)

编辑:

更多信息 DOCUMENT_ROOT 和其他 PHP服务器变量可以在这里找到

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.jpg to go from the top directory down), PHP actually accesses the server root when you use 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:

/home2/siteuserftp/public_html/test/

"test" represents your site root

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.

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

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

The answer Paul Dixon provided:

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)

EDIT:

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

濫情▎り 2024-08-11 05:30:01

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

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

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

你试图从你的 *nix 机器的根目录包含。

虽然有多种方法可以完成您正在做的事情,但保罗·迪克森的建议可能是您最好的选择。

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

So, when you do something like this

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

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

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

完美的未来在梦里 2024-08-11 05:30:01

每个网络服务器都有一个 public_html 文件夹,您通常在其中保存文件等。通过使用 /,您将不会到达 public_html,而是您直接指向主(无法访问)根。因此,请使用 $_SERVER['DOCUMENT_ROOT']."/your/locati.on"

Every web server has a public_html folder, 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

逆光飞翔i 2024-08-11 05:30:01

我在运行 Windows 和 IIS 的计算机上通过以下方法解决了这个问题:

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

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

127.0.0.1   www.example.com

另外,您需要在 php.ini 中启用allow_url_include,如下所示

allow_url_include = On

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

<?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

127.0.0.1   www.example.com

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

allow_url_include = On
阳光下慵懒的猫 2024-08-11 05:30:01

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

  1. 将 C:\Inetpub\wwwroot 的 subst 复制到驱动器。设为W:(WEB内容)。
    subst W: C:\Inetpub\wwwroot
  1. 这样编辑 php.ini:将 W:\ 附加到 include_path,将 doc_root 更改为 W:\
include_path = ".;c:\php\active\includes;W:\"
doc_root = W:\

  1. 将 subst 命令放入 Startup 文件夹中的 CMD 文件中以自动进行映射。

现在,两个版本都允许:

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

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

  1. do subst of C:\Inetpub\wwwroot to a drive. Let it be W: (WEB contents).
    subst W: C:\Inetpub\wwwroot
  1. edit php.ini this way: append W:\ to include_path, change doc_root to 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.

Now, both versions allowed:

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

两仪 2024-08-11 05:30:01

某些版本的 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');
    }

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:

    $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');
    }
云雾 2024-08-11 05:30:01

也许这有点不传统,

如果我有一个像

/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

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

像这样

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

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

/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 

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"

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 
半窗疏影 2024-08-11 05:30:01

这个答案并不是真正针对根目录,但一种解决方法是使用 ../ 跳转到父目录。
当然,您需要了解这种方法的文件结构.

例如,您可以使用:

include('../parent.php');
include('../../grand_parent.php');

This answer is not really for the root directory, but one workaround is to use ../ to jump to the parent directory.
Of course, you need to know the file structure for this approach though.

For example, you could use:

include('../parent.php');
include('../../grand_parent.php');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文