简体   繁体   中英

root path doesn't work with php include

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

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')

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

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.

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

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

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');
    }

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 

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM