PHP: How to get only the current folder of a website
<p>For example I have a website that points to a page like this:</p>

<pre><code>http://www.mysite.com/folder/file
</code></pre>

<p>How can I get determine /folder so that I can go further an quote an if statement like</p>

<pre><code>if /folder then echo something
</code></pre>

<p>Why do I need this?</p>

<p>I am trying to tell facebook which image to pick from a page. Actually I have a pretty simple page structure and the image that facebook should take is always at first but somehow it does choose another one from time to time. I guess because the other images are loaded faster. And the old way to rel="img_src" doesn't seem to work anymore as that I could just add it to the wanted image.</p>

<p>So well of course I use the open graph protocol to tell facebook which Image it should use.</p>

<p>I am working with a cms were I can output the path of the image depending on the id the image has. I have two different id's for the different kind of pages living in two different folders.</p>

<p>This leads to:</p>

<pre><code>if index --&gt; echo meta og for index img
else if /folderone (with id1) --&gt; echo meta og for id1
else if /foldertwo (with id2) --&gt; echo meta og for id2
</code></pre>

<p>This is why I need to know the foldername.</p>

<p>Now with the answer I have following setup, just that you know:</p>

<pre><code>&lt;?php $folder = dirname($_SERVER['SCRIPT_NAME']); ?&gt;

&lt;?php if (dirname($_SERVER['SCRIPT_NAME']) == "/") echo "&lt;meta property='og:image' content='http://www.mysite.com/img/img.jpg'/&gt;" ;?&gt;
&lt;?php if (dirname($_SERVER['SCRIPT_NAME']) == "/folderOne") echo "&lt;meta property='og:image' content='http://www.mysite.com/img/{$img_id1}'/&gt; " ;?&gt;
&lt;?php if (dirname($_SERVER['SCRIPT_NAME']) == "/folderTwo") echo "&lt;meta property='og:image' content='http://www.mysite.com/img/{$img_id2}'/&gt; " ;?&gt;
</code></pre>
