Root directories.

Post all your tuts or request for tuts here.
Post Reply
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Root directories.

Post by Torniquet »

I find one of the major pains about coding, is accessing parent directories.

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");
 
This is all fine and dandy, but especially in game sites like the ones shown on this very website, you may need to include many files on every page.

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");
so 5 files we need to include at the top of everypage. we can access the root directory easy enough by adding ../ on the front of all of those not a problem.

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 .= '../';
}
 
This tiny snipet of code gets the number of folders it has to go back through to reach the root directory, then compiles them into a series of '../../../' easily allowing you to dynamically reach the root directory without having to count out how many times you need to place a single ../

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");
and now all you have to do is include the single config.php file to the top of every one of your pages, and still be able to access all of these scripts.

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
New Site Coming Soon! Stay tuned :D
djand3y93
Posts: 16
Joined: Tue Nov 02, 2010 6:20 pm

Re: Root directories.

Post by djand3y93 »

Thanks, I wanted to know how to do something like this, 'cuz I got a lot of problems when I include scripts from folders.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Root directories.

Post by Xaleph »

Having a trailing forward slash means pretty much the same. However, you still need to give the path to the file itself. But then again, that`s mostly strictly seperated in Java ( be it Spring or whatever)

However, in PHP it can be a pain in the ass, I usually define all root paths. This includes the domain ( http://website.com ). The domain is not needed, in fact, not recommended, so for file traversing or dir traversing, I don`t use it.

But what I do is :

define('ROOT', "Application/");
define("INCLUDES", ROOT."Includes/");

etc etc.

This way, you can allways use defined constants, which can come in handy. Mostly because defined constants are accessible on all subsequent pages. I`ll admit, it`s a bit more work, however, I dynamically load most scripts to begin with. So having a good foundation is OK, having a strong foundation is king.
Post Reply

Return to “Tutorials”