note 109813 added to function.include
| From: | StephenL. | Date: | Thu, 23 Aug 2012 15:26:51 +0000 |
| Subject: | note 109813 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190195@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 82.236.106.11
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- https://master.php.net/note/edit/109813
Del: integrated -- https://master.php.net/note/delete/109813/integrated
Del: useless -- https://master.php.net/note/delete/109813/useless
Del: bad code -- https://master.php.net/note/delete/109813/bad+code
Del: spam -- https://master.php.net/note/delete/109813/spam
Del: non-english -- https://master.php.net/note/delete/109813/non-english
Del: in docs -- https://master.php.net/note/delete/109813/in+docs
Del: other reasons-- https://master.php.net/note/delete/109813
Reject -- https://master.php.net/note/reject/109813
Search -- https://master.php.net/manage/user-notes.php