Thursday, January 27, 2005

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

3 Comments:

Anonymous Anonymous said...

I found it too. very useful.

8:18 AM  
Anonymous Nick said...

Yes useful, thanks.

9:27 AM  
Anonymous Jonathan said...

This works for me:

$dir = dirname(__FILE__);

3:07 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home