jrcarr Posted October 29, 2006 Share Posted October 29, 2006 I'm trying to figure out how to get the location that a file is executed. For example, if I run a script in www.domain.com/test and have the following code:[code]<?echo $_SERVER['DOCUMENT_ROOT']. "<br>";echo $_SERVER['SCRIPT_FILENAME']. "<br>";echo $_SERVER['SERVER_NAME']. "<br>";echo $_SERVER['SCRIPT_NAME'];[/code][b]I get the following information:[/b]/home/canter??/public_html /home/canter??/public_html/test/phpinfo.php www.domain.com /test/phpinfo.php Now the info I need is, which is the actual location of the file being run:/home/canter??/public_html/test ($_SERVER['SCRIPT_FILENAME'] minus the file name and the ending slash)http://www.domain.com/test ($_SERVER['SERVER_NAME']+$_SERVER['SCRIPT_NAME'] minus the file name and the ending slash)I know this can't be that difficult, but I'm am drawing a total blank. ThanksJack Quote Link to comment Share on other sites More sharing options...
Destruction Posted October 29, 2006 Share Posted October 29, 2006 [code]$scriptLocation = basename(dirname($_SERVER['SCRIPT_FILENAME']));[/code]I believe would work, not sure if there's another way that's "easier". I'll explain:dirname(file) will retrieve the name of the directory the file is in but will be in full ie: /home/sites/name/public_html/basename will retrieve the 'base' of either a filename or directory ie: basename(index.php) returns indexbasename(/home/sites/name/public_html) returns public_htmlHope this helps,Dest Quote Link to comment Share on other sites More sharing options...
jrcarr Posted October 29, 2006 Author Share Posted October 29, 2006 Ok, how about taking the info from $_SERVER['SERVER_NAME'], which in the example given would be "/home/canter??/public_html/test/phpinfo.php" and find and remove everything from the last accurance of the "/" to the end, leaving only "/home/canter??/public_html/test". If the script/file is run at: "/home/canter??/public_html/phpinfo.php" it would leave "/home/canter??/public_html"I'm just not sure what php command would look through a string and find the "last" accurance of a character and then replace everything from it to the end with "".Then with server name, I could do something like the following, using the "basename" like you mentioned:[code]$scriptLocation = "http://" . ($_SERVER['SCRIPT_FILENAME']). "/" .basename(dirname($_SERVER['SCRIPT_FILENAME']));[/code]Using the first example with a "test" directory, it should give me:[code]http://www.domain.com/test[/code]But if sitting in the root directory, it wouldn't work, since it would read like:[code]http://www.domain.com/public_html[/code]I can check for whether basename(dirname($_SERVER['SCRIPT_FILENAME'])) = "public_html"; and not include it, since not all servers are setup the same and it won't always be "public_html".Does any of this make sense and is it as difficult as I'm trying to make it?Jack Quote Link to comment Share on other sites More sharing options...
Destruction Posted October 29, 2006 Share Posted October 29, 2006 Okay, if you're using it in a sense that's used for external links you may wish to use something with an external location and not an absolute 'internal' path. Perhaps using REQUEST_URI instead of SCRIPT_FILENAME.HTHDest Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.