note 97284 added to function.getcwd

From: Date: Mon, 12 Apr 2010 13:18:49 +0000
Subject: note 97284 added to function.getcwd
Groups: php.notes 
Request: Send a blank email to php-notes+get-168152@lists.php.net to get a copy of this message
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(); ---- Server IP: 91.199.167.223 Probable Submitter: 192.87.137.47 ---- Manual Page -- http://www.php.net/manual/en/function.getcwd.php Edit -- https://master.php.net/note/edit/97284 Del: integrated -- https://master.php.net/note/delete/97284/integrated Del: useless -- https://master.php.net/note/delete/97284/useless Del: bad code -- https://master.php.net/note/delete/97284/bad+code Del: spam -- https://master.php.net/note/delete/97284/spam Del: non-english -- https://master.php.net/note/delete/97284/non-english Del: in docs -- https://master.php.net/note/delete/97284/in+docs Del: other reasons-- https://master.php.net/note/delete/97284 Reject -- https://master.php.net/note/reject/97284 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168152) next »