This is a discussion on problem trying to detect what folder a file resides in within the PHP Language forums, part of the PHP Programming Forums category; I am trying to auto-detect what folder a file is sitting in and based upon this info, query a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|