root path doesn't work with php include
Solution 1
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')
Solution 2
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
Solution 3
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.
Solution 4
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
Solution 5
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
Related videos on Youtube
Cœur
Everybody should contribute to clean up Stack Overflow. SO is intended to be a top-quality Q&A site, meant not just for the OP, but for posterity. Thanks to search engines, questions and answers become authoritative for the whole Internet. --Paul Draper TODO: disambiguate the 18,300+ duplicate titles from 41,600+ questions fix the uneditable titles (1,117 titles with length < 15) fix the uneditable titles (containing "help", "problem", "question", "doubt", …) fix the uneditable messages (containing "mydomain.com", "domain.com", "mysite.com", "site.com", "abc.com", "xyz.com", …) fix the uneditable messages with link shorteners (5,032 url:goo.gl, 3,673 url:bit.ly, 1,982 url:tinyurl.com, 1,748 url:cl.ly, …) remove the dead images/codes (8,051 url:imageshack.us, 2,818 url:pastie.org, 2,307 url:photobucket, 430 url:skitch.com, 214 url:rapidshare.com, 78 url:paste.ofcode.org, 58 url:expirebox.com, 4 url:megaupload.com, …) fix the broken links in messages, broken links to connect.microsoft.com, … review the potentially broken Apple links: #DOCUMENTATION in the URL, /library but not /archive in the URL, url:developer.apple.com/mac/library, url:developer.apple.com/safari/library rollback the 99+ solved, resolved, fixed, answered titles (meta, alternative query) correct the spelling in titles correct the 6,600+ "thanks in advanced" and 1,100+ "thanks in advice", …
Updated on December 23, 2020Comments
-
Cœur over 3 years
/ 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?
-
Eric J. over 14 yearsAlso make sure the user running php has access to that folder.
-
EllaJo almost 13 yearsWhen 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?
-
Damien Golding about 11 yearsAnybody 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 <img src="./img1.jpg" />
-
Srikanth Kondaparthy over 10 yearsThis 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.
-
Soundfx4 almost 9 yearswould 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 almost 9 yearsNvm, 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
-
Andrey Baluevsky about 7 yearsInstead 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"
-
MR_AMDEV almost 5 yearsThe starting slash works for
stylesheets
/script
/href
tags