Page 1 of 1
Need Help: Dynamically knowing base level directory
Posted: Thu May 25, 2006 6:05 pm
by mjhamson
I have been trying to solve a problem, and I am not sure of the best way.
I want to have a global variable that represents the path of the website. So, if the path is at the root level (i.e. htdocs), it would be "/". If the user placed the web application at a sublevel folder (i.e. ~username) then it would be /~username.
I have no probelm in doing this so long as i know if the path is in a sub folder. What I am having a problem with is knowing if the root level is actual or not across all pages. The entry point for the web application can be anywhere, so I am afforded the ability to define this at login (there is no login).
I know about the $_SERVER array of variables, but there are no combinations that I can see that can define this aspect. If however it is understood that the web application is a sub folder of the root.. then yes... its easy. But again, no way to tell if you are root or not if you are in a sub directory of the actual web application.
Any help... is very much appreciated.
-Michael
Posted: Thu May 25, 2006 6:15 pm
by RobertGonzalez
You might try looking at
realpath()
Posted: Thu May 25, 2006 7:04 pm
by mjhamson
Everah,
thank you for your reply. The problem is that with realpath (as well as other methods) , you still do not have a means for knowing if the first folder is the root level of the application. (unless i am missing something major). I hope i have explained the situation well enough. It is not that I need the full path (which can be gotten simply using php_self), but rather in being able to understand dynamicaly the starting path of the web applications.
For example, we have a web application called Foo. the user puts this application at ~user/myprivatesite/Foo.
It is easy to understand that the path to the document Foo/warranties/electronics.php is in actuality ~user/myprivatesite/Foo/warranties/electronics.php.
However, what we need to know is that the actual application root path is ~/user/myprivatesite/Foo.
Now if a user puts the Foo web application in his root, the path for electronics.php would then be
~user/warranties/electronics.php
i hope this explains my delima better now. Sorry for the confusion.. and really appreciate you speaking up.
-Michael
Posted: Thu May 25, 2006 7:39 pm
by RobertGonzalez
mjhamson wrote:However, what we need to know is that the actual application root path is ~/user/myprivatesite/Foo.
Now if a user puts the Foo web application in his root, the path for electronics.php would then be
~user/warranties/electronics.php
Unless this is a typo, I don't quite understand. You can get the root path from the home director by using
getcwd(). In your example above, it looks like you are referencing a created or pseudo folder (again, unless that's a typo).
Re: Need Help: Dynamically knowing base level directory
Posted: Fri May 26, 2006 3:16 am
by xpgeek
mjhamson wrote:I have been trying to solve a problem, and I am not sure of the best way.
I want to have a global variable that represents the path of the website. So, if the path is at the root level (i.e. htdocs), it would be "/". If the user placed the web application at a sublevel folder (i.e. ~username) then it would be /~username.
I have no probelm in doing this so long as i know if the path is in a sub folder. What I am having a problem with is knowing if the root level is actual or not across all pages. The entry point for the web application can be anywhere, so I am afforded the ability to define this at login (there is no login).
I know about the $_SERVER array of variables, but there are no combinations that I can see that can define this aspect. If however it is understood that the web application is a sub folder of the root.. then yes... its easy. But again, no way to tell if you are root or not if you are in a sub directory of the actual web application.
Any help... is very much appreciated.
-Michael
The award is :
Code: Select all
$docroot = $_SERVER['DOCUMENT_ROOT'];
//SCRIPT_FILENAME or PHP_SELF or REQUEST_URI or PATH_TRANSLATED
$cwd = $_SERVER['SCRIPT_FILENAME'];
$path = str_replace($docroot, "", $cwd);
echo dirname($path);
Posted: Fri May 26, 2006 9:06 am
by mjhamson
xpgeek,
thanks for your valant attempt. However after having a full nights sleep, I think I have realized that the only way possible to solve my problem is as follows.
create a single file with a unique name. Somethink like "rootfolderdefinition" or use a guid. Then, when a user loads a page on the site, regardless of where they are... we check first to see if a session variable or some other superglobal type has been set that tells us the root folder. If not, then we crawl back out of the directory structure until we find the "rootfolderdefinition" file. When this happens, then we can set the values that we need. Once this is set, then its a simple and quick var reference. As such, then the web application truly becomes portable.
In my current scheme, i am trying to use two variables that represent the full path to a file, and a "backpath" (i.e. ../../) value for the user accessed file.
-Michael