0

I've been wandering for a while trying to get out of this problem.

I have a php software in which I want to know where the script is being executed.

e.g.

I have /foo/bar/home.php . In this case I would like to know that /foo/bar is my root. But if I have an example admin page /foo/bar/admin/index.php I would like to have /foo/bar in this case too. Or

/foo/bar/foo/index.php -> /foo/bar/foo /foo/bar/foo/randomname/home.php -> /foo/bar/foo

How can I accomplish that ?

Thanks for help

2 Answers 2

1

Because /foo/bar does not really sound like a filesystem path to me, I assume that this is the first part of the URL path.

How does the application know that this prefix is applied to everything? It must be installed somewhere, and if no rewriting is applied, the filesystem path layout and url path layout match on some level. This might be used to actually generate path information by subtracting some strings, but I doubt it's usefulness.

I'd opt for defining a constant "INSTALL_PATH" and/or "INSTALL_URL" in a file that is included everywhere, which knows its relative location to the base url or file path, and does a simple string operation:

define('INSTALL_PATH', basename(__DIR__); // go one level up

Sign up to request clarification or add additional context in comments.

1 Comment

You pointed was I was going to do, and I do have an idea about, I was just wondering which is the "straightest" way to do it. Infact I'm using two constants for path and url in order to get proper inclusions and redirects. By the way it helped me a lot knowing I'm on the right way. Thanks a lot.
1

You can access the properties of the $_SERVER superglobal variable.

echo $_SERVER['DOCUMENT_ROOT'];

The following, if located in "/foo/bar/foo/index.php", would echo /foo/bar/foo/.

2 Comments

+1. For clarification, if you're in "/foo/bar/foo/subdirectory/index.php", it would still echo /foo/bar/foo/ if that's your web server's document root. Do a var_dump($_SERVER); to see all possibilities.
I didn't want the / or the www root. $_SERVER['DOCUMENT_ROOT'] has the absolute root. I needed to have my application's root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.