Using 'require_once' on files in a directory above current

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
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Using 'require_once' on files in a directory above current

Post by mcccy005 »

Basically I have a 'global_functions.php' file which has a bunch of handy little functions (including connecting to a specific database) which I use in heaps of different files so that if I change anything in it (eg. the password or name of the database), then I only have to change it once.

Anyways, if I have the following directory structure:

D:\Web_Pages\Objects\Results.php

Inside Results.php, I want to access 'global_functions.php' using require_once.
And yep, sure I can go 'require_once('d:/Web_Pages/global_functions.php') (or however it goes); but I want the name of the drive to be transparent so I dont have to change things when I move it onto a web server.
The directory structure would remain the same from so I wasn't sure if I could somehow just call 'localhost\global_functions.php' and this would work on any web server (assuming my home directory was set to D:\Web_Pages).

Thanks.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

if your directory is d:\Web_pages and you have a folder below called Objects where your global_functions.php file is then:

Code: Select all

require_once ('Objects/global_functions.php');
should do it
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

All paths in PHP are relative to the document root. If you like you can check what this is by echoing $_SERVER['DOCUMENT_ROOT']. Then from that you can see whatever you need to add to get where you want to. Remember .. goes to the parent directory if you need to do that. i.e. if doc root is

Code: Select all

/web/bigprojs/yeknom
and you want

Code: Select all

/web/objects/file.php
you would do

Code: Select all

../../objects/file.php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

getcwd() may be of interest...
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Just to clarify, the current working directory might be D:\Web_pages\Objects\results\results.php
and I want to call 'global_functions.php' which is in D:\web_pages\global_functions.php

So if my web server (IIS) home directory is set to D:\web_pages\; are you guys saying that in PHP, this would be the document root so I only have to call 'global_functions.php'?

And if for some reason 'global_functions.php' was in D:\web_pages\objects\, I would call '\objects\global_functions.php'?

(I can't actually try this right at this point in time which is why I need to clarify completely because I don't have internet access where I have the php etc. installed; but I don't have php etc. installed here where I have web access)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

There is no way php can automagically just know where your file is.

However, there is, of course, the ini approach.


Basically, you can create an ini file that holds your folder information.

In php, use parse_ini to obtain this information. This will define your directories to now be set as the current path.


then, all you have to do is include('whateverfile.php'); and php will know, via your ini settings in the file you created, just where to look. However, it will look in the order in which you define your directory paths. So, if you have 5 copies, the first one it finds is the one it will use.
Post Reply