Click to See Complete Forum and Search --> : file Root directory
Vasilli
06-27-2003, 07:13 PM
Hi,
this is one of my first attempts at PHP and I am trying to find a way of telling PHP that my root directory is say "www.mydomain.com/"
The reason for this is so that if I have many include files in multiple directories i.e. /includes/anotherfolder/, /includes/yetanotherfolder/ or even /calculators/ I can tell the include/string or whatever to look in the root directory as apposed to the include/ directory (I hope this makes since)
it is difficult to explain why I need such a function but it would make life 100x easier if there was such a function. Please help me if you can
Thanks
PHP n00bie
Try something like this (the else if part probably doesn't work as this is untested code and I don't know how to get the length of an array in PHP):
$currdir = $_SERVER['REQUEST_URI'];
$second = split("/", $currdir)[2]; // if it doesn't work try 1
if($second == "/"){$currdir = "../";}
//else if(split("/", $second) == 2){$currdir = "../";}
else { $currdir = ""; }
And then everywhere you include a file use this (for an example .js file):
<script src="<? echo $currdir;?>/includes/file.js"></script>
[J]ona
Well, your root directory won't look like www.yourdomain.com, it will be more like /path/to/your/public_html. But, you can get both, anyway:
$_SERVER["DOCUMENT_ROOT"]; #will give you /path/to/your/public_html
$_SERVER["HTTP_HOST"]; #will give you www.yourdomain.com
Originally posted by Jona
...and I don't know how to get the length of an array in PHP$array_length = count($arrayname);
Thanks, pyro. I knew it was something like that. All right, so I the below could should work--hopefully:
$currdir = $_SERVER['REQUEST_URI'];
$second = split("/", $currdir)[2]; // if it doesn't work try 1
if($second == "/"){$currdir = "../";}
else if(count(split("/", $second)) == 2){$currdir = "../";}
else { $currdir = ""; }
[J]ona
Vasilli
06-27-2003, 08:25 PM
Thankyou both for your thought/fixes
but i don't quite understand how that will work
let me try to explain a little better what i am trying to do.
ok
i have a file called index.php and what i want to do, using this code below, is to include a file in the "includes/" directory (which are includes that will be used in alot of the pages.)
if (isset($HTTP_GET_VARS['page']))
{
$file = $HTTP_GET_VARS['page'];
include('includes/'. $file);
}
else include('$_SERVER["HTTP_HOST"; includes/index.inc.php');
which is fine if the file calling the include is in the root directory, the problem is that i want to be able to uses these same files for other pages which will live in other directories i.e. "services/partners/" or whatever. so all i should need to do in a link situation (<a href="ROOT includesfile1.inc.php">Click me to inclued</a>) as apposed to ../../includesfile1.inc.php
I am a beginer, and have a very limited knowledge of php and i would appreciate it if you could show me what i need to do. because i didn't realy understand how to implement what u mentioned above to make it work :(
thank you for your help sofar, and any thing further!
I see. Try something like this:
if (isset($HTTP_GET_VARS['page'])) {
$file = $HTTP_GET_VARS['page'];
include($SERVER["DOCUMENT_ROOT"]."includes/". $file);
}
else {
include($_SERVER["DOCUMENT_ROOT"]."includes/index.inc.php");
}
Vasilli
06-27-2003, 08:41 PM
Well what can i say?
You have saved me hours and hours of time, A million thankyou's to you all, especially Pyro for saving me!!!!
have a great resto of the day/night
:D
thank you
I'm happy to have been able to help. :) Let us know if there is anything else we can do...