root path doesn't work with php include

后端 未结 8 1575
天命终不由人
天命终不由人 2020-12-03 00:56

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

相关标签:
8条回答
  • 2020-12-03 01:05

    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

    0 讨论(0)
  • 2020-12-03 01:06

    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.

    0 讨论(0)
  • 2020-12-03 01:06

    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');
        }
    
    0 讨论(0)
  • 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
    
    
    0 讨论(0)
  • 2020-12-03 01:19

    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 
    
    0 讨论(0)
  • 2020-12-03 01:23

    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
    
    0 讨论(0)
提交回复
热议问题