title: PHP dirname() loses last folder
body: <h2>The Problem</h2>
  
  <p>This is a very simple-to-understand question.</p>
  
  <p>I'm having a user submit a URL, for example "http://example.com/path/filename.html".</p>
  
  <p>I'm using PHP's <code>dirname()</code> function to get the so-called "base" of this URL. For the above example, that would be "http://example.com/path".</p>
  
  <p>My problem arises when the user enters this:</p>
  
  <pre><code>http://example.com/blog
  </code></pre>
  
  <p>If you type the above into your browser, you will see the index.php or .html page in the folder called "blog". However, PHP's <code>dirname()</code> will return only "http://example.com".</p>
  
  <p>I'm not sure if it thinks that "blog" is an extension-less file, if that exists, but I can't really find a solution.</p>
  
  <h2>Things I've Tried</h2>
  
  <p>I first tried getting the extension of the URL using this quick method:</p>
  
  <pre><code>$url = 'http://example.com/index.php';
  $file_extension = end(explode('.', $filename));
  </code></pre>
  
  <p>Then, I would check if the extension existed using PHP <code>empty()</code>. If the extension exists, that means that a filename was entered after the folder, such as "http://example.com/path/file.html", and <code>dirname()</code> is perfect. If the extension doesn't exist, no file was entered and the last item in the path is a folder, so it is already "the base".</p>
  
  <p>However, in the case of simply "http://example.com/path/", the above would return ".com/path/" as the file extension, which we all know doesn't exist. In this case, I would use the <code>dirname()</code> function and cut off "/path/".</p>
  
  <p>EDIT:</p>
  
  <p>Taking the extension of <code>basename($url)</code> won't work because if the user enters "http://example.com" <code>basename()</code> returns "example.com", the extension for which is supposedly 
  ".com"</p>
  
  <h3>Hopefully, someone has had the same problem and knows the solution. I'm still looking, but any answers are wholly appreciated!!</h3>