Jump to content

How do I get the current directory?


OM2

Recommended Posts

Can someone tell me how I get JUST the current directory of the calling script?

 

I've looked up and there a few options: but I end with the full directory location

 

All I want is *just* the directory name

 

I would have thought that there would be a constant?

 

I've come across the following script a few times while googling:

 

function getCurrentDirectory() {

  $path = dirname($_SERVER['PHP_SELF']);

  $position = strrpos($path,'/') + 1;

  return substr($path,$position);

}

 

Is this the only or best way to get the current directory name?

 

Thanks

 

 

OM

Link to comment
https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/
Share on other sites

thanks for the quick reply

 

getcwd() gives the full path, the same as dirname(__FILE__) for example

 

lets say one of these calls gives me:

 

/home/username/dir1/dir2/dir3

 

what i'm saying is that ALL i want is: dir3

 

let me know if u can suggest anything else

 

thanks

 

 

om

You strip out the $_SERVER["DOCUMENT_ROOT"] from the getcwd() string, which would give the current directory relative to the web root

 

Or explode the string returned by getcwd() into an array, and take only the last element

u mean something like:

 

function getCurrentDirectory() {

  $path = dirname($_SERVER['PHP_SELF']);

  $position = strrpos($path,'/') + 1;

  return substr($path,$position);

}

 

i was just trying to avoid having to write a function making a function call

i assumed there would be a constant available?

 

thanks

i was just trying to avoid having to write a function making a function call

i assumed there would be a constant available?

Well the current directory isn't constant you can change it

 

$currentDirectory = array_pop(explode('/',getcwd()));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.