SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
May 11, 2009, 08:44 #1
- Join Date
- Apr 2009
- Location
- Porto,Portugal
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
best way to deal with includes file paths
Hi.
I need to include a file into some other files that are in different directories.
I have made a config_php file that contains a defined variable BASE_URI which the path to the root folder of my webiste.
The problem is to include the config_php the relative path for the varies from page to page (pages in different folders).
Should i use an absolute path to that file.or there is a best way?
PS:When i try to use absolute path in localhost i get an error:
"URL file-access is disabled in the server configuration "
How to change that?
-
May 11, 2009, 08:47 #2PHP Code:
include '/config.php';
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
May 12, 2009, 01:28 #3
Surely that wont have the desired effect? For example, the path could be...
/something/something/scriptlocation
In this situation, it would look for config.php in the first "/" directory, rather than "scriptlocation".
I'm having a similar problem. I want to know how to make a web app work no matter what directory it is installed in. I thought the way to get around that would be to store the path to the script installation in a config file, but then that file itself needs to be included which is where it gets confusing.
-
May 12, 2009, 01:50 #4
Of course. I was assuming his config file would be in the root directory. In your example, the include would be
PHP Code:include '/something/something/config.php';
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
May 13, 2009, 02:53 #5
- Join Date
- Apr 2009
- Location
- Porto,Portugal
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I didnt know that if i put /... it will start from the root directory.
Thanks. That should work.
I love this forum
Edit: Strange. If i put require_once '/Site/config/config.php'; it doesn´t work.
If i put $_SERVER['DOCUMENT_ROOT']. '/Site/config/config.php it works.
I am using wamp and my server root is C:/wamp/www.
-
May 13, 2009, 06:31 #6
- Join Date
- Oct 2008
- Location
- London
- Posts
- 862
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Pawel Decowski (you should follow me on Twitter)
-
May 13, 2009, 10:27 #7
- Join Date
- Jul 2007
- Location
- Denmark
- Posts
- 162
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In my projects, Im usually using a constant based on a file or a folder I know exists. Usually its my "Includes" folder
This is placed in the configuration file, which ofcourse is included on every page of my website.
The code is simple, and works like a charm:
Code PHP:
No matter how deep into your file structure you are, the for loop moves back through the folders, looking for the directory "Includes" and when it locates the folder, the contents of the variable "$root" is placed in the constant "ROOT".
Because its a constant, it will work in functions aswell.
Example of usage:
Code PHP:include(ROOT.'Includes/Navigation.php');
Ofcourse one can easily be creative, and define constants for each folder, making it easy to locate specifik folders without risking typo's
Code PHP:
Another neat bonus, is that I can test if ROOT is not defined, and make sure no one directly accesses any file that should have been included.
Simply place this line at the top of the file:
The only drawback I can think of, is that I need to know the location of the configuration file where the ROOT constant is defined, but I think it's a relative easy thing to keep track of.zalucius
-
May 13, 2009, 12:33 #8
- Join Date
- Jun 2006
- Posts
- 638
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 13, 2009, 13:27 #9
- Join Date
- Jul 2007
- Location
- Denmark
- Posts
- 162
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just like you normally would...
Imagine we are in the administration of the website, the page requested resides in the admin folder, giving a path like this: "Administration/edit_something.php"
The configuration file is located in the "Includes" folder, giving it a path like this "Includes/configuration.php"
The edit_something.php would then include the configuration like this:
Code PHP:require_once('../Includes/configuration.php'); require_once(ADMIN.'some_file_in_admin_folder.php'); // just as an example
If the requested file was in a subfolder of a subfolder of the includes folder, then the path would look like this:
Code PHP:require_once('../../configuration.php'); require_once(IMAGES.'some_picture.jpg'); // just as an example
zalucius
Bookmarks