How do I get the name of the current subdirectory?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
St8ic
Forum Newbie
Posts: 16
Joined: Thu Dec 09, 2004 10:40 pm

How do I get the name of the current subdirectory?

Post by St8ic »

I need an expression to figure out what the current subdirectory is in this fashion:

http://www.mysite.com/ <--- /
http://www.mysite.com/folder/ <--- /folder/
http://www.mysite.com/folder/index.php <--- /folder/

Anyone have any ideas?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: How do I get the name of the current subdirectory?

Post by yacahuma »

There should be a better way, I think. But this works

Code: Select all

$p = parse_url($url, PHP_URL_PATH);
echo '/' . basename(substr($p,0,strrpos($p,'/')));
St8ic
Forum Newbie
Posts: 16
Joined: Thu Dec 09, 2004 10:40 pm

Re: How do I get the name of the current subdirectory?

Post by St8ic »

thanks for your repy Yacahuma. It looks like it may work, but does anyone have anything a little more elegant?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: How do I get the name of the current subdirectory?

Post by yacahuma »

yes. you will think their is a function for that. But At least all the ones I tried basename,dirname,pathinfo, did not work for your particular problem
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do I get the name of the current subdirectory?

Post by McInfo »

This is a somewhat hacky solution, but it's compact.

Code: Select all

echo (($n = basename(dirname($_SERVER['SCRIPT_NAME']))) ? "/$n/" : "/");
Let me know if I need to explain anything.
St8ic
Forum Newbie
Posts: 16
Joined: Thu Dec 09, 2004 10:40 pm

Re: How do I get the name of the current subdirectory?

Post by St8ic »

Thanks for your reply and for future readers, this is the expression I ended up using :)

Code: Select all

dirname($_SERVER['PHP_SELF']) . "/"
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do I get the name of the current subdirectory?

Post by McInfo »

You should really use SCRIPT_NAME because PHP_SELF is SCRIPT_NAME with possible garbage appended. If the file name in the URI is followed by a slash and one or more characters, dirname() can be tricked into giving an incorrect result.

example.php

Code: Select all

<p><a href="example.php/junk">Append Junk</a></p>
<pre>SCRIPT_NAME: <?php echo $_SERVER['SCRIPT_NAME'], PHP_EOL; ?>
PHP_SELF:    <?php echo $_SERVER['PHP_SELF'], PHP_EOL; ?>
dirname:     <?php echo dirname($_SERVER['PHP_SELF']); ?></pre>
Using SCRIPT_NAME can prevent cross-site scripting in some cases.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: How do I get the name of the current subdirectory?

Post by MindOverBody »

Version 5.3 of PHP brought interesting/elegant solution for this: __dir__
In older versions just use: basename( dirname(__file__) );
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do I get the name of the current subdirectory?

Post by McInfo »

MindOverBody wrote:Version 5.3 of PHP brought interesting/elegant solution for this: __dir__
In older versions just use: basename( dirname(__file__) );
That works as long as the script is not in the document root. In that case, that solution would result in something like "htdocs" or "www" rather than "" as per St8ic's specification because, unlike $_SERVER['SCRIPT_NAME'], the magic constants __DIR__ and __FILE__ are relative to the filesystem root rather than the document root.
Post Reply