By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,664 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,664 IT Pros & Developers. It's quick & easy.

problem trying to detect what folder a file resides in

P: n/a
I am trying to auto-detect what folder a file is sitting in and based
upon this info, query a MySQL database to get further information. I
have been all over php.net and been through numerous examples, but I
can't get my script to do exactly what I want...

An example given on php.net regarding dirname() is as follows:
dirname can be used to create self referencing web scripts with the
following one liner.

$base_url = str_replace($DOCUMENT_ROOT, "", dirname($PHP_SELF));

Using this method on a file such as:

/home/mysite/public_html/wherever/whatever.php

will return:

/wherever
This code could very well be right, but it's not working on my localhost
or on my live server... instead of returning only the folder in
question, it returns the full path to the file. Going with the above
example, if "home" were specified as the server, it would return
/mysite/public_html/wherever instead of simply "wherever". I have also
tried echoing $DOCUMENT_ROOT and $PHP_SELF to see exactly what I have to
work with and trying to manipulate these values, but I can't seem to
make it work with other methods I have tried either. Thanks very much
in advance for any help anyone can provide.

Marcus

Jul 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mon, 30 Jun 2003 02:08:15 -0500, Marcus wrote:
I am trying to auto-detect what folder a file is sitting in and based upon
this info, query a MySQL database to get further information. I have been
all over php.net and been through numerous examples, but I can't get my
script to do exactly what I want...

An example given on php.net regarding dirname() is as follows:
dirname can be used to create self referencing web scripts with the
following one liner.

$base_url = str_replace($DOCUMENT_ROOT, "", dirname($PHP_SELF));

Using this method on a file such as:

/home/mysite/public_html/wherever/whatever.php

will return:

/wherever
This code could very well be right, but it's not working on my localhost
or on my live server... instead of returning only the folder in question,
it returns the full path to the file. Going with the above example, if
"home" were specified as the server, it would return
/mysite/public_html/wherever instead of simply "wherever". I have also
tried echoing $DOCUMENT_ROOT and $PHP_SELF to see exactly what I have to
work with and trying to manipulate these values, but I can't seem to make
it work with other methods I have tried either. Thanks very much in
advance for any help anyone can provide.

Marcus

Have you tried setting
$DOCUMENT_ROOT = "/home/mysite/public_html"
before that bit of code you have?
Then it will change /home/mysite/public_html/ with nothing, so you'd have
/wherever in this example, but when trying on a file like
/home/mysite/public_html/wherever/whatever/myfile.php you'd get
/wherever/whatever

If you'd only want /whatever in that case, you could use some
stringfunctions to cut of everyting before the last / then you'd have
/whatever

.... hope this helps
Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.