Page 1 of 1
How do I get the name of the current subdirectory?
Posted: Tue Aug 24, 2010 7:48 pm
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?
Re: How do I get the name of the current subdirectory?
Posted: Tue Aug 24, 2010 9:09 pm
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,'/')));
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 12:54 am
by St8ic
thanks for your repy Yacahuma. It looks like it may work, but does anyone have anything a little more elegant?
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 4:43 am
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
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 12:11 pm
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.
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 5:52 pm
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']) . "/"
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 6:37 pm
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.
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 7:42 pm
by MindOverBody
Version 5.3 of PHP brought interesting/elegant solution for this: __dir__
In older versions just use: basename( dirname(__file__) );
Re: How do I get the name of the current subdirectory?
Posted: Wed Aug 25, 2010 7:59 pm
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.