note 58578 deleted from function.dirname by betz
| From: | betz@php.net | Date: | Sat, 12 Nov 2005 12:18:32 +0000 |
| Subject: | note 58578 deleted from function.dirname by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-98477@lists.php.net to get a copy of this message | ||
Note Submitter: jamespam at hotmail dot com
Reason: useless
----
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.)