Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP require file from top directory

Tags:

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?

like image 354
Lucas Penney Avatar asked Oct 02 '11 23:10

Lucas Penney


People also ask

HOW include file in parent directory in php?

If the referenced object is a folder, the function will return the parent folder of that folder. For the current folder of the current file, use $current = dirname(__FILE__); . For a parent folder of the current folder, simply use $parent = dirname(__DIR__); .

What is __ DIR __ in php?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

What is dirname (__ file __)?

dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap. php resides. (Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__) .)

How can I get root directory path in php?

In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.


2 Answers

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.

like image 180
CheeseSucker Avatar answered Nov 28 '22 09:11

CheeseSucker


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

like image 30
JWC May Avatar answered Nov 28 '22 08:11

JWC May