Simple Machines Community Forum

General Community => Scripting Help => Topic started by: jrcarr on October 29, 2006, 01:30:12 PM

Title: find location a script/file is running in?
Post by: jrcarr on October 29, 2006, 01:30:12 PM
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:


<?
echo $_SERVER['DOCUMENT_ROOT']. "<br>";
echo $_SERVER['SCRIPT_FILENAME']. "<br>";
echo $_SERVER['SERVER_NAME']. "<br>";
echo $_SERVER['SCRIPT_NAME'];


I get the following information:
/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.  Thanks

Jack
Title: Re: find location a script/file is running in?
Post by: SleePy on October 29, 2006, 03:08:47 PM
Try:

echo dirname(__FILE__);
echo basename($_SERVER['SCRIPT_FILENAME']);


See what results you get  ;)

that should help with what your trying to do..
Title: Re: find location a script/file is running in?
Post by: jrcarr on October 29, 2006, 03:44:56 PM
Ok, the code
echo dirname(__FILE__);

gives me "/home/canter??/public_html" if script/file is in the root and "/home/canter??/public_html/test" if the script/file is in the /test folder.  That works great for that part of my needs.

The code:
echo basename($_SERVER['SCRIPT_FILENAME']);
Just gives me the file name off the end and at this time this info I'm not needing.

However, I still need to determine the URL of the folder the script/file is located in and display it as:
http://www.domain.com if the script/file is in the root directory or http://www.domain.com/test if the script/file is in the /test folder or.....http://www.domain.com/test/dir1/dir2 if the script/file is located in the /dir2 folder.

What I'm trying to do is create an Installation routine that determines where the program has been uploaded and create a setting file based on that location.

Jack
Title: Re: find location a script/file is running in?
Post by: SleePy on October 29, 2006, 04:01:05 PM
I know theres a easier way just can't remember..

but this should work.. didn't test but it should


echo $urlpath = str_replace(basename($_SERVER['SCRIPT_FILENAME']), '' ,$_SERVER['SERVER_NAME']. $_SERVER['SCRIPT_NAME']);

When your done testing you can remove the echo..

that should remove the filename out of the combined of $_SERVER['SERVER_NAME']+$_SERVER['SCRIPT_NAME']
but will leave a trailing /
Title: Re: find location a script/file is running in?
Post by: jrcarr on October 29, 2006, 04:18:55 PM
That will work great, I just have to adjust the rest of the code to allow for the ending slash, which shouldn't be tough, it a small program, not like SMF.  :D

Thanks a lot

Jack
Title: Re: find location a script/file is running in?
Post by: SleePy on October 29, 2006, 04:21:03 PM
hmm.. well maybe we can fix that..

try:

echo $urlpath = str_replace('/'.basename($_SERVER['SCRIPT_FILENAME']), '' ,$_SERVER['SERVER_NAME']. $_SERVER['SCRIPT_NAME']);


Title: Re: find location a script/file is running in?
Post by: jrcarr on October 29, 2006, 05:05:24 PM
Thanks, that worked fine and all is well now.  You are a scholar and a gentleman.

Jack