PHP: get absolute path to current directory
This is just handy enough to put down here, as I'm sure I'll need this like 3 weeks from now and won't know quite where to find it:
$dir_path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", dirname(realpath(__FILE__)) ) . DIRECTORY_SEPARATOR;
Hopefully I'll remember I put it here.
It returns the path for an include file when used in an included file. For the main file calling the script, I think you use $_SERVER['SCRIPT_NAME'] in place of __FILE__. Something like that
$dir_path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", dirname(realpath(__FILE__)) ) . DIRECTORY_SEPARATOR;
Hopefully I'll remember I put it here.
It returns the path for an include file when used in an included file. For the main file calling the script, I think you use $_SERVER['SCRIPT_NAME'] in place of __FILE__. Something like that
3 Comments:
I found it too. very useful.
Yes useful, thanks.
This works for me:
$dir = dirname(__FILE__);
Post a Comment
Subscribe to Post Comments [Atom]
<< Home