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.
Using 'require_once' on files in a directory above current
Moderator: General Moderators
if your directory is d:\Web_pages and you have a folder below called Objects where your global_functions.php file is then:
should do it
Code: Select all
require_once ('Objects/global_functions.php');- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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 and you want you would do
Code: Select all
/web/bigprojs/yeknomCode: Select all
/web/objects/file.phpCode: Select all
../../objects/file.phpJust 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)
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)
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.
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.