note 109813 deleted from function.include by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 15:44:49 +0000 |
| Subject: | note 109813 deleted from function.include by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190326@lists.php.net to get a copy of this message | ||
Note Submitter: Stephen L.
----
Including relative paths is indeed a lot of headaches.
The solution proposed by Rick Garcia 08-May-2008 09:38 is quite good with the prepend.php script,
but if you don't want to do that, you can do something else:
Either:
include(dirname(__FILE__).'/../myinclude.php'); // BEST
Or simply:
include('/../myinclude.php'); // GOOD
In this case, don't EVER forget the prepended '/' in the path, because doing this is
WRONG:
include('../myinclude.php'); // WRONG
This is particularly useful when you're doing a subinclude of a file in a parent folder. Eg:
include1.php
include4.php
include [folder]
---- include2.php
---- include3.php
with:
include1.php
<?php
print('include1 here!');
include('/include/include2.php');
?>
include2.php
<?php
print('<br />include2 here!');
include('include3.php'); // this works
//include('include/include3.php'); // This will ALSO work, because we in include2.php we
have two include path: the root path, and /include/ folder. This way is in fact the right way to do
so.
include(dirname(__FILE__).'/../include4.php');
//include('/../include4.php'); // Also works
//include('../include4.php'); // Does NOT work
//include('include4.php'); // Will also work, interesting, this is because the basepath is
in the executing script
?>
include3.php
<?php
print('<br />include3 here!');
?>
include4.php
<?php
print('<br />include4 here!');
?>
This will print:
include1 here!
include2 here!
include3 here!
include4 here!
Discussion:
You can see that the interesting stuff is in include2.php. There are two things happening:
- we have two include paths, but this only works for filenames, not for paths (eg:
include('include3.php') only includes a file, it will search in both include paths,
include('include/include3.php') has a path, it will search only from the base path of the
executing script ).
- this tricky behaviour explains why in the second case with include4.php, it doesn't work: we
specify a path in the include, and thus it only search from the base path, hence why
include('include4.php') works, but NOT include('../include4.php'), because then
PHP will in fact search from the base path of the executing script, and won't use the secondary
include path '/include/' !
dirname() is then a good solution, and I guess that prepending '/' is a quick shortcut (if
someone has a fuller explanation, I'll be glad to hear).
Last note: these tests have been done inside a subfolder, not from the very root, thus
'/include/' was in fact '/test/include/' on my host. This means that this
peculiar behaviour does not happen only on root directory but on any directory.
Conclusion: either avoid relative paths, but if for example you're making a standalone module
and would like to be able to freely move it around, then use dirname(), this will ease your life a
lot.