timmah1 Posted December 14, 2006 Share Posted December 14, 2006 Hello, How do you get the name of the directory that you are in?For example, if type in the address bar (http://www.domain.com/test), how could a PHP script detect that your in the directory of test?I hope that makes senseThanks Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 $_SERVER['PHP_SELF'] will give you the folder and script in reference to to the domainI do not think there is a function to return just the folder you are in, but here is something you could use[code]<?phpfunction directory($dir){$folder = substr($dir, 0 ,strpos($dir, strrchr($dir, "/")));return $folder;}echo directory($_SERVER['PHP_SELF']);?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
SharkBait Posted December 14, 2006 Share Posted December 14, 2006 what about [code=php:0] dirname($_SERVER['PHP_SELF']); [/code] ? Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 14, 2006 Share Posted December 14, 2006 Don't forget, that [code=php:0]$_SERVER['PHP_SELF'][/code] will only give you the path in reference to the domain, not the server.If you want the full path, you'll need to use something like [code=php:0]dirname($_SERVER['PATH_TRANSLATED'])[/code] RegardsHuggie Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2006 Author Share Posted December 14, 2006 Great! Thanks.Now that I have it stating the directory, how do I carry that value to an sql statement?I keep getting errors, most likely have something backwards, but I'm really not sure.Here's the code[code]$sql = "SELECT * FROM users WHERE username = '{directory($_SERVER[PHP_SELF]}'";$result = mysql_query($sql);while($row = mysql_fetch_array( $result )){ echo $row[email]; echo $row['email']; }[/code]I'm not getting the email value pulled out.Anyone have an idea?Thanks again Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 you can do 2 things, put the value into a variable or come out of your sql statement.[code]$dir = directory($_SERVER['PHP_SELF']);$sql = "SELECT * FROM users WHERE username = '$dir'";[/code]or[code]$sql = "SELECT * FROM users WHERE username = '".directory($_SERVER['PHP_SELF'])."';[/code]Ray Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2006 Author Share Posted December 14, 2006 I'm sorry to keep bothering you, because you have been awesome!I'm only having one small problem, when the directory is selected, it's putting a "/" in front of it, that's why it can't pull the info from the database.How do I get rid of the "/" in front of the $dir? Quote Link to comment Share on other sites More sharing options...
SharkBait Posted December 14, 2006 Share Posted December 14, 2006 You can [code=php:0]substr($dir, 1, strlen($dir));[/code] So it starts at character 1 instead of character 0 which is the / Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 so you just want the directory name and nothing else??changed a little[code]<?phpfunction directory($dir){$folder = substr(dirname($dir),1);return $folder;}echo directory($_SERVER['PHP_SELF']);?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 14, 2006 Author Share Posted December 14, 2006 You are incredible craygo!!Thank you so, so much!!! Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 14, 2006 Share Posted December 14, 2006 as for me, $_SERVER['PHP_SELF'] would give you /forum/folder/test.phpand dirname($_SERVER['PHP_SELF']) would give you /forum/folderis there any possibility if i want the folder part of the string and nothing else?Thanks Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 15, 2006 Share Posted December 15, 2006 can anyone give me suggestions on this please? Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 15, 2006 Share Posted December 15, 2006 ted_chou12,Please don't 'hijack' threads. You made a post relating to this yesterday which I replied to.Did you even try what I suggested yesterday in your other post, you haven't updated it, so are we to assume you haven't? If so, why are you still seeking solutions when you may already have what you need? If you have and it didn't work, then please post back in the original thread ([url=http://www.phpfreaks.com/forums/index.php/topic,118583.0.html]problem with listing root directories (UN~SOLVED)[/url])Huggie Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 15, 2006 Share Posted December 15, 2006 nope , this one is not the same as the one yesterday, I want to achieve the name of the directory this time, because i want to show it on my navigator. The other one was problem with listing the files in the folder. Quote Link to comment Share on other sites More sharing options...
craygo Posted December 15, 2006 Share Posted December 15, 2006 yes just search for the last "/" and give you everything after[code]<?phpfunction directory($dir){$folder = substr(strrchr(substr (dirname($dir),1), "/"), 1);return $folder;}echo directory($_SERVER['PHP_SELF']);?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 15, 2006 Share Posted December 15, 2006 thankyou! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.