note 58578 added to function.dirname
| From: | jamespam at hotmail dot com | Date: | Tue, 08 Nov 2005 19:25:25 +0000 |
| Subject: | note 58578 added to function.dirname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98268@lists.php.net to get a copy of this message | ||
Here's a function to get the relative path of an included file:
The return path is relative to the script that is called.
so...
/somedir/test.php
that includes a file:
"/other_dir/include/incfile.inc"
will get "../other_dir/include/" if the function is called from incfile.inc:
rel_path_for_included_file(__FILE__);
function rel_path_for_included_file($magic_file_constant){
$included_from_root=str_replace($_SERVER['DOCUMENT_ROOT'] .
"/","",dirname($magic_file_constant));
$root_from_script=dirname($_SERVER['SCRIPT_NAME'])=='/' ? './' :
str_repeat("../", substr_count(dirname($_SERVER['SCRIPT_NAME']),
"/"));
return $root_from_script.$included_from_root."/";
}
I use this within object class files that include other supporting files. It gets gid of the need
to specify an INSTALL_DIRECTORY variable for my utilities.
Please let me know if there's a better way to do this.
(I think the __FILE__ constant may require some help on windows.)
----
Manual Page -- http://www.php.net/manual/en/function.dirname.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58578
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58578&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58578&report=yes
Search -- http://master.php.net/manage/user-notes.php