0

Possible Duplicate:
PHP Get name of current directory

Today I tried to get the folder name of a directory. I was successful to an extent. But it did not meet my criteria. The code which I have is as follows.

<?php
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
$foldername = basename($url);
echo $foldername;
?>

This returns the final value in an URL. I mean if the URL is localhost/default it returns default and when the url is localhost/default/index.php it returns index.php. I want it to return default in both the cases.

So is it possible to get the thing which I am expecting ?? Please help me out with that..

Community
  • 1
  • 1
Srivathsan
  • 600
  • 3
  • 9
  • 19

2 Answers2

4

predefined contstants

You are probably looking for __DIR__

if you are looking for the directory in the URL rather than the file system, you could also use

$path_parts = pathinfo($_SERVER['PATH_INFO']);
$dir = $path_parts['dirname'];
Trey
  • 5,480
  • 4
  • 23
  • 30
2
<?php 

$url = "uour_url";
$array = explode('/',$url);
$count = count($array);
echo $array[$count-2];

?>

OR you can use getcwd(). This Function will return current directory.

I have not tested it but hope it will helpful to you

Hardik
  • 1,429
  • 2
  • 19
  • 37