note 97284 deleted from function.getcwd by danbrown
| From: | danbrown@php.net | Date: | Mon, 12 Apr 2010 14:53:55 +0000 |
| Subject: | note 97284 deleted from function.getcwd by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168154@lists.php.net to get a copy of this message | ||
Note Submitter: boukeversteegh at gmail dot com
----
As noted, dirname( __FILE__ ) returns the path of the script from which the function is called.
Which is different from getcwd(), or relatives paths in general, in case that your script is
included from another directory.
The following function cd() has the same result as dirname( __FILE__ ). It uses a little bit of
magic to make it work. You can include the definition of this function from other files, and use it
from included files, it will still work.
cd.php
<?php
# Current Directory
function cd( ) {
# magic
$trace = debug_backtrace();
# get the filename of the script that called this function. E.g. the last item on the stack.
$localpath = dirname( $trace[0]['file'] );
return realpath( $localpath );
}
?>
usage:
<?php
include cd() . '/subdir/script.php'; # instead of include
'subdir/script.php'
?>
The easiest is to put cd.php in the path of your PATH variable, and include it like "include
cd.php". From then you can use cd();