Absolute path

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nicofast
Forum Newbie
Posts: 3
Joined: Fri Mar 24, 2006 1:47 pm

Absolute path

Post by Nicofast »

Hi!

I'm a newbie with PHP, and I'm creating my web. The problem is that I need the home path of the server to call my functions, independently where is the call to them. Anyone can help me?

Thank you!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

There are many ways to achieve that. Here are a couple:

Code: Select all

define('BASE_DIR', '/path/to/my/files/');

include BASE_DIR . 'myscript.php';
Or

Code: Select all

define('BASE_DIR', '/path/to/my/files/');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . BASE_DIR);

include 'myscript.php';
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could potentially use $_SERVER['DOCUMENT_ROOT'] too
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

feyd wrote:could potentially use $_SERVER['DOCUMENT_ROOT'] too
Take Feyd's advice. This way if you ever switch servers you won't have to worry about changing the document root :)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

or use both ? :)

Code: Select all

$path = $_SERVER['DOCUMENT_ROOT'] . BASEURL;
header('Location: $path');
(in which BASEURL is not an absolute server path of course)

Or is that stupid? :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ignoring the redirection, it's fine. I do it myself in many projects.
Nicofast
Forum Newbie
Posts: 3
Joined: Fri Mar 24, 2006 1:47 pm

Thank you to all

Post by Nicofast »

Thank you, for all the replies :D
Post Reply