27

I have several subdomains contained in their own directory above my root site, and an assets folder in my root directory. For example:

/
/assets/
/forums/
/blog/

I'm trying to require() files on php pages in both the root directory, and the subdirectories, for example, assets/include/form.php is required() in both index.php in the root directory and index.php in the forums directory. However, inside form.php, is another require, trying to get another file contained in the include directory.

I can't seem to find a way to require a file inside this form.php where it will work on both the root directory's index.php and the forum directory's index.php.

Is there any way I can explicitly require a file starting from the root directory?

Lucas Penney
  • 2,624
  • 4
  • 27
  • 36

6 Answers6

61

There are several ways to achieve this.

Use relative path from form.php

require_once __DIR__ . '/otherfile.php';

If you're using PHP 5.2 or older, you can use dirname(__FILE__) instead of __DIR__. Read more about magic constants here.

Use the $_SERVER['DOCUMENT_ROOT'] variable

This is the absolute path to your document root: /var/www/example.org/ or C:\wamp\www\ on Windows.

The document root is the folder where your root level files are: http://example.org/index.php would be in $_SERVER['DOCUMENT_ROOT'] . '/index.php'

Usage: require_once $_SERVER['DOCUMENT_ROOT'] . '/include/otherfile.php';

Use an autoloader

This will probably be a bit overkill for your application if it's very simple.

Set up PHP's include paths

You can also set the directories where PHP will look for the files when you use require() or include(). Check out set_include_path() here.

Usage: require_once 'otherfile.php';


Note:

I see some answers suggest using the URL inside a require(). I would not suggest this as the PHP file would be parsed before it's included. This is okay for HTML files or PHP scripts which output HTML, but if you only have a class definition there, you would get a blank result.

dodov
  • 5,206
  • 3
  • 34
  • 65
CheeseSucker
  • 1,270
  • 1
  • 9
  • 13
  • do you do any cleaning / validation of `$_SERVER['DOCUMENT_ROOT']` in case it contains evil characters? – Steve Nov 10 '12 at 01:02
  • 2
    @Steve: No. This is a variable provided by the system your application is running on, so it is not provided by the user. If this variable contains "evil characters" then you have a configuration issue or your system has been hacked. Neither of these are anything your application can do much about. – CheeseSucker Nov 11 '12 at 22:03
3

If one level higher use:

include('../header.php');

if two levels higher:

include('../../header.php');

Fore more, visit: http://forums.htmlhelp.com/index.php?showtopic=2922

JWC May
  • 605
  • 8
  • 14
0
  • suppose you have pgm1.php in www.domain.com/dir1/dir2/dir3/pgm1.php, which include or require 'dirA/inc/Xpto.php'.
  • and another pgm2.php in www.domain.com/dirZ1/dirZ2/pgm2.php, which include or require same 'dirA/inc/Xpto.php'.
  • and your Host configuration not authorize require/include 'http://www.domain.com/......'
  • So...you can use this, in both pgm1.php & pgm1.php, and REQUIRE or
    INCLUDE works !

    function rURI($p){
       for($w='',$i=1;$i<(count(explode('/',$_SERVER['PHP_SELF']))-1);$i++,$w.='../');return
       $w.$p; }
    

    require rURI('dirA/inc/Xpto.php'); // path from root

msrd0
  • 7,816
  • 9
  • 47
  • 82
0

Maybe this isn't correct topic and maybe not correct use, but maybe it will help someone. When I wanted to make links in menu from absolute path, as I was jumping from directory to directory and paths were nonstop changing, I wanted to fix to base dir.

So instead of using '../' to go sub-folder (as I didn't know many I needed to go down ../../../), I wrote link as <a href='/first_dir/second_dir/third_dir//..'>, similar it works also with <img src='/first_dir/second_dir//../logo.svg'>. Unfortunately this doesn't work with require_once(). PHP 7

Ecko
  • 31
  • 2
0

You have 2 solutions. You can either use the full URL to the file, for instance if your file is in the 'lib' directory you can use: require_once ('http://www.example.com/lib/myfile.php')

or

You can try the $_SERVER['DOCUMENT_ROOT'] function in PHP but this isnt always fool proof.

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
xXPhenom22Xx
  • 1,265
  • 5
  • 29
  • 63
-3

why don't you use (../)

require('../folder/file.php');
Ahmad Yousef
  • 621
  • 9
  • 20
  • 3
    Because your solution is wrong (you cannot use `require` in this way; please, read the other answers and understand its proper usage) and, additionally, this question was asked 4 years ago and got much better and comprehensive answers? Please, before answering here make sure that you are understanding the whole situation properly; what means: understanding the problem + knowing how to solve it + understanding what the other answers are already providing. Additionally, answering so old questions (with accepted answers) is rarely a good idea. – varocarbas Nov 11 '15 at 08:25