SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: path issues
-
Aug 13, 2007, 08:30 #1
- Join Date
- Aug 2005
- Posts
- 102
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
path issues
Hello All
I am currently reworking a past project and am getting rid of most of the procedural code and replacing it with classes. everything is going quite well but the paths are driving me nuts. I am trying to use one consistent solution for all links and include files but every variation I try seems to have an issue somewhere. I will run down what I have tried and see what your suggestions are.
1) page relative:
Code:include('../classes/myClass.php');
2) root relative i.e.
Code:include('/myproject/classes/myClass.php');
Code:include('/myproject/classes/myClass.php'); myClass = new myClass;
3) absolute paths
Code:include('c:/wamp/www/myProject/classes/myClass.php');
Does that make sense?
Thanks
Matt
-
Aug 13, 2007, 11:43 #2
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can set the include_path in php.ini (or an .htaccess file) and then include relative to that.
You aren't including relative in that example. If you prefix with a slash, you're including from the root (Eg. C:\). To include relative, you should start with the dirname. The reason, you're getting an error, is because include doesn't fail, if the file isn't found. You should rarely use include or include_once -- Use require and require_once instead.
-
Aug 13, 2007, 11:55 #3
- Join Date
- Jul 2006
- Location
- Prague, Czech republic
- Posts
- 16
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My favourite solution, script relative:
PHP Code:require dirname(__FILE__) . "/path/relative/to/the/script/where/it/is/called/from.php";
PHP Code:// include /foo/something.php
require dirname(__FILE__) . "/../something.php";
-
Aug 13, 2007, 12:35 #4
- Join Date
- May 2007
- Location
- Poole, UK
- Posts
- 5,077
- Mentioned
- 103 Post(s)
- Tagged
- 0 Thread(s)
my preferred way is:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/path/to/file/thefile.php'; ?>
-
Aug 14, 2007, 21:44 #5
- Join Date
- Aug 2005
- Posts
- 102
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks guys, 2 of the three ways I was not familiar with and 1 of the three ways I tried didn't work. I will give it another pass.
As an aside to kyberfabrikken, I was using require_once initially and I was reading in some php book written by some zend guy that include is more efficient than require as it is only "included" when it is needed compared to require which all the requires are loaded regardless of if they are needed or not.
Just thought I would throw that out there.
Thanks again;.
Matt
-
Aug 15, 2007, 00:53 #6
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Premature optimisation is the root of all evil. Well not all perhaps, but close enough.
require_once has the functionality you need -- include doesn't. That should be sufficient reason to pick the first. In regard to performance, include and require are identical. To quote the manual:
The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error.
-
Aug 15, 2007, 11:39 #7
- Join Date
- Aug 2005
- Posts
- 102
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I know, I read the manual - but the zend guy in the book said.... oh well.
I like the root of all evil quote.
Got everything working again. I had multiple issues - I went the $_SERVER['DOCUMENT_ROOT'] route since that was the quickest for me to get going.
I was using a relative path in my session login redirect so that is why that wasn't working.
Thanks again.
Matt
Bookmarks