views:

2642

answers:

10

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.

Thanks!

+2  A: 

../../index.php

bdukes
+1  A: 

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

../../../ is three. etc

Ólafur Waage
+1  A: 

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

HAXEN
+13  A: 

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

../../index.php

This would be two directories up.

Konrad Rudolph
+1  A: 

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

MazarD
+1  A: 
../../../includes/boot.inc.php

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

leek
+1  A: 
. = 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.

Joe Skora
A: 

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)

ZombieSheep
+7  A: 

To include a file one directory back, use "../file".
For two directories back, use "../../file".
This extends on indefinitely.

Though realistically, you shouldn't be including relative to the current directory - what if you wanted to move that file? All of the links would break. A very way to ensure that you can still reliably link to other files while retaining those links if you move your file is:

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

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

Dan Hulton
A: 

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.mysite.com/game/forum/files/index.php you can add an include to /includes/boot.inc.php which would resolve to http://www.mysite.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.

Mauro