How do I include a file over 2 directories back?

PhpIncludeFilesystems

Php Problem Overview


How do you include a file that is more than 2 directories back. I know you can use ../index.php to include a file that is 2 directories back, but how do you do it for 3 directories back? Does this make sense? I tried .../index.php but it isn't working.

I have a file in /game/forum/files/index.php and it uses PHP include to include a file. Which is located in /includes/boot.inc.php; / being the root directory.

Php Solutions


Solution 1 - Php

.. selects the parent directory from the current. Of course, this can be chained:

../../index.php

This would be two directories up.

Solution 2 - Php

To include a file one directory back, use '../file'. For two directories back, use '../../file'. And so on.

Although, realistically you shouldn't be performing includes relative to the current directory. What if you wanted to move that file? All of the links would break. A way to ensure that you can still link to other files, while retaining those links if you move your file, is:

require_once($_SERVER['DOCUMENT_ROOT'] . 'directory/directory/file');

DOCUMENT_ROOT is a server variable that represents the base directory that your code is located within.

Solution 3 - Php

. = current directory
.. = parent directory

So ../ gets you one directory back not two.

Chain ../ as many times as necessary to go up 2 or more levels.

Solution 4 - Php

include dirname(__FILE__).'/../../index.php';

is your best bet here, and it will avoid most of the relative path bugs you can encounter with other solutions.

Indeed, it will force the include to always be relative to the position of the current script where this code is placed (which location is most likely stable, since you define the architecture of your application). This is different from just doing include '../../index.php' which will include relatively to the executing (also named "calling") script and then relatively to the current working directory, which will point to the parent script that includes your script, instead of resolving from your included script's path.

From the PHP documentation:

> Files are included based on the file path given or, if none is given, > the include_path specified. If the file isn't found in the > include_path, include will finally check in the calling script's own > directory and the current working directory before failing.

And the oldest post I've found citing this trick dates back to 2003, by Tapken.

You can test with the following setup:

Create a layout like this:

htdocs
¦   parent.php
¦   goal.php
¦
+---sub
    ¦   included.php
    ¦   goal.php

In parent.php, put:

<?php
include dirname(__FILE__).'/sub/included.php';
?>

In sub/included.php, put:

<?php
print("WRONG : " . realpath('goal.php'));
print("GOOD : " . realpath(dirname(__FILE__).'/goal.php'));
?>

Result when accessing parent.php:

WRONG : X:\htdocs\goal.php
GOOD : X:\htdocs\sub\goal.php

As we can see, in the first case, the path is resolved from the calling script parent.php, while by using the dirname(__FILE__).'/path' trick, the include is done from the script included.php where the code is placed in.

Beware, the following NOT equivalent to the trick above contrary to what can be read elsewhere:

include '/../../index.php';

Indeed, prepending / will work, but it will resolve just like include ../../index.php from the calling script (the difference is that include_path won't be looked afterwards if it fails). From PHP doc:

> If a path is defined — whether absolute (starting with a drive letter > or \ on Windows, or / on Unix/Linux systems) or relative to the > current directory (starting with . or ..) — the include_path will be > ignored altogether.

Solution 5 - Php

../ is one directory, Repeat for two directories ../../ or even three: ../../../ and so on.

Defining constants may reduce confusion because you will drill forward into directories verses backwards

You could define some constants like so:

define('BD', '/home/user/public_html/example/');

define('HTMLBD', 'http://example.com/');

When using 'BD' or my 'base directory' it looks like so:

file(BD.'location/of/file.php');

define(); reference

Solution 6 - Php

../../index.php 

       

Solution 7 - Php

../../../includes/boot.inc.php

Each instance of ../ means up/back one directory.

Solution 8 - Php

Try This

this example is one directory back

require_once('../images/yourimg.png');

this example is two directory back

require_once('../../images/yourimg.png');

Solution 9 - Php

following are ways to access your different directories:-

./ = Your current directory
../ = One directory lower
../../ = Two directories lower
../../../ = Three directories lower

Solution 10 - Php

You can do ../../directory/file.txt - This goes two directories back.

../../../ - this goes three. etc

Solution 11 - Php

../../../index.php

         

Solution 12 - Php

if you are using php7 you can use dirname function with level parameter of 2, for example :

dirname("/usr/local/lib", 2);

the second parameter "2" indicate how many level up

dirname referance

Solution 13 - Php

I recomend to use __DIR__ to specify current php file directory. Check here for the reason.

__DIR__ . /../../index.php

Solution 14 - Php

But be VERY careful about letting a user select the file. You don't really want to allow them to get a file called, for example,

../../../../../../../../../../etc/passwd

or other sensitive system files.

(Sorry, it's been a while since I was a linux sysadmin, and I think this is a sensitive file, from what I remember)

Solution 15 - Php

../../../includes/boot.inc.php

Solution 16 - Php

including over directories can be processed by proxy file

  • root

  • .....|__web

  • .....|.........|_requiredDbSettings.php

  • .....|

  • .....|___db

  • .....|.........|_dbsettings.php

  • .....|

  • .....|_proxy.php

     dbsettings.php:
     $host='localhost';
     $user='username':
     $pass='pass';
    
     proxy.php:
     include_once 'db/dbsettings.php
    
     requiredDbSettings.php:
     include_once './../proxy.php';
    

Solution 17 - Php

if you include the / at the start of the include, the include will be taken as the path from the root of the site.

if your site is http://www.example.com/game/forum/files/index.php you can add an include to /includes/boot.inc.php which would resolve to http://www.example.com/includes/boot.inc.php .

You have to be careful with .. traversal as some web servers have it disabled; it also causes problems when you want to move your site to a new machine/host and the structure is a little different.

Solution 18 - Php

Try ../../. You can modify it accordingly as it will take you up back two directories. First reach to root directory then access the required directory.

E.g. You are in root/inc/usr/ap and there is another directory root/2nd/path. You can access the path directory from ap like this: ../../2nd/path first go to root than desired directory. If not working please share.

Solution 19 - Php

I saw your answers and I used include path with syntax

require_once '../file.php'; // server internal error 500

and http server (Apache 2.4.3) returned internal error 500.

When I changed the path to

require_once '/../file.php'; // OK

everything is fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcusView Question on Stackoverflow
Solution 1 - PhpKonrad RudolphView Answer on Stackoverflow
Solution 2 - PhpDan HultonView Answer on Stackoverflow
Solution 3 - PhpJoe SkoraView Answer on Stackoverflow
Solution 4 - PhpgaborousView Answer on Stackoverflow
Solution 5 - Phpim_brian_dView Answer on Stackoverflow
Solution 6 - PhpbdukesView Answer on Stackoverflow
Solution 7 - PhpleekView Answer on Stackoverflow
Solution 8 - Phprajpoot rehanView Answer on Stackoverflow
Solution 9 - Phpuser2951753View Answer on Stackoverflow
Solution 10 - PhpÓlafur WaageView Answer on Stackoverflow
Solution 11 - PhpMazarDView Answer on Stackoverflow
Solution 12 - PhpronView Answer on Stackoverflow
Solution 13 - PhpLF00View Answer on Stackoverflow
Solution 14 - PhpZombieSheepView Answer on Stackoverflow
Solution 15 - PhpHAXENView Answer on Stackoverflow
Solution 16 - PhpallennView Answer on Stackoverflow
Solution 17 - PhpMauroView Answer on Stackoverflow
Solution 18 - PhpkhanView Answer on Stackoverflow
Solution 19 - PhpBronekView Answer on Stackoverflow