as everyone should know, you can access parent directories via the use of '../../' etc infront of the directory x amount of levels down.
So here is a simple file structure.
root
--|
--folder 1
--|
--folder2
----|
----afile.php
--|
--folder3
----|
----folder sub directory1
------|
------anotherfile.php
in order for us to reach afile.php from anotherfile.php we need to do this.
Code: Select all
include ("../../folder2/afile.php");
so for arguments sake we have to include a minium of 5 extra scripts which all have their individual purposes into every page...
Code: Select all
include("scripts/db_connect.php");
include("scripts/player_stats.php");
include("classes/battle.class.php");
include("classes/enemies.class.php
include("languages/english.php");
but, wouldnt it be nice to keep them all (along with various other config operations) all locked away in a single file? I am sure, many people wil agree that the answer is yes. Then all you have to worry about is including that single file, lets call it config.php and make it sit on the root directory.
The problem we face with this, is that there is no simple way (like javascript and HTML which i will show you at the end) of connecting to the root directory easily.
So as soon as you include this new config.php file into a file in a folder or 2 deep, we run into a problem, because it looking for those additional files within that files parent directory.
Well, I have worked out how we can utilize this new config file and connect to any include scripts, no matter how deep into folders we are whether it be 10, 20 or 30 sub folders in.
Code: Select all
$str = explode('/',$_SERVER['SCRIPT_NAME']);
$to_root = count($str) - 2;
for($x=0; $x<$to_root; $x++){
$root_dir .= '../';
}
Have this placed at the top of your config file, and all is wonderful.
your new config file will now look like this.
Code: Select all
$str = explode('/',$_SERVER['SCRIPT_NAME']);
$to_root = count($str) - 2;
for($x=0; $x<$to_root; $x++){
$root_dir .= '../';
}
include($root_dir ."scripts/db_connect.php");
include($root_dir ."scripts/player_stats.php");
include($root_dir ."classes/battle.class.php");
include($root_dir ."classes/enemies.class.php
include($root_dir ."languages/english.php");
WONDERFUL!
I know this may seem long winded... but hopfuly it explains the aim of the snippet and what it achives.
HOW FOR A QUICK ROOT DIRECTORY TIP FOR HTML/CSS AND JAVASCRIPT.
did you know you can access the root directory without all the ../../../ nonsense?
Well you can.
simply put a single / in the front of the directory you want to access, be it an image, javascript or css folder and no matter how deep down you are, you will access the correct folder
a file 30 folders deep containing <img src="/images/diamond.gif" /> will access the image as if the file was sitting right there on the root directory.
handy much? i think so!
enjoy and peace out x