News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

find location a script/file is running in?

Started by jrcarr, October 29, 2006, 01:30:12 PM

Previous topic - Next topic

jrcarr

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
Jack Carr

SleePy

#1
Try:

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


See what results you get  ;)

that should help with what your trying to do..
Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

jrcarr

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
Jack Carr

SleePy

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 /
Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

jrcarr

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
Jack Carr

SleePy

hmm.. well maybe we can fix that..

try:

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


Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

jrcarr

Thanks, that worked fine and all is well now.  You are a scholar and a gentleman.

Jack
Jack Carr

Advertisement: