49

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

Cœur
  • 37,241
  • 25
  • 195
  • 267

9 Answers9

69

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')
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Also make sure the user running php has access to that folder. – Eric J. Sep 09 '09 at 16:53
  • When I try the $_SERVER['DOCUMENT_ROOT'], I am getting " unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING ". Do you know why? – EllaJo May 17 '11 at 13:24
  • Anybody know if there is a way to set the root for includes? For example: include_path = "mysite.com/images/"; include "./content/index.php"; //Below is inside /content/index.php // I want it to get mysite.com/images/img1.jpg – Damien Golding May 01 '13 at 04:43
  • This doesnt work with IIS server. As $_server['document_root'] is not available in PHP installed on windows IIS server. I added a solution for IIS server but its deleted by moderator. – Srikanth Kondaparthy Sep 05 '13 at 23:22
  • would it not be an acceptable solution to simply traverse the directories? The root of the file system should remain the same, and unless you're running a dedicated machine, your http root folder should also stay the same (I think?). So couldn't you do, for example, /home/username/public_html/ ? Is it a bad idea to have that path written on you pages where anyone can read it maybe? – Soundfx4 Jun 21 '15 at 00:24
  • Nvm, I got my answer from another question on here. Shared hosting, they can move your webs root directory at anytime, which would break that path :| whelp...so much for that :D – Soundfx4 Jun 21 '15 at 00:46
35

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

Ian
  • 3,266
  • 4
  • 29
  • 37
5

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.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
5

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

2

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
David
  • 68
  • 1
  • 7
1

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

  • Instead of "subst" command, use "persistent subst" by tweaking registry this way: `REGEDIT4 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices] "W:"="\\??\\C:\\Inetpub\\wwwroot"` – Andrey Baluevsky Mar 17 '17 at 12:56
0

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');
    }
user1123382
  • 120
  • 6
0

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

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');
SafalFrom2050
  • 687
  • 6
  • 12