177

I have a php page inside a folder on my website.

I need to add the name of the current directory into a variable for example:

$myVar = current_directory_name;

Is this possible?

hakre
  • 193,403
  • 52
  • 435
  • 836
Satch3000
  • 47,356
  • 86
  • 216
  • 346

9 Answers9

331
getcwd();

or

dirname(__FILE__);

or (PHP5)

basename(__DIR__) 

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.dirname.php

You can use basename() to get the trailing part of the path :)

In your case, I'd say you are most likely looking to use getcwd(), dirname(__FILE__) is more useful when you have a file that needs to include another library and is included in another library.

Eg:

main.php
libs/common.php
libs/images/editor.php

In your common.php you need to use functions in editor.php, so you use

common.php:

require_once dirname(__FILE__) . '/images/editor.php';

main.php:

require_once libs/common.php

That way when common.php is require'd in main.php, the call of require_once in common.php will correctly includes editor.php in images/editor.php instead of trying to look in current directory where main.php is run.

Oliver Nybo
  • 560
  • 1
  • 6
  • 24
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
31

To get only the name of the directory where script executed:

//Path to script: /data/html/cars/index.php
echo basename(dirname(__FILE__)); //"cars"
yesnik
  • 4,085
  • 2
  • 30
  • 25
19

You can use dirname(__FILE__) to get the path to the directory of the current file.

Example: /path_to/your_dir/your_file.php:

// use dirname to get the directory of the current file
$path = dirname(__FILE__);
// $path here is now /path_to/your_dir

// split directory into array of pieces
$pieces = explode(DIRECTORY_SEPARATOR, $path);
// $pieces = ['path_to', 'your_dir']

// get the last piece
echo $pieces[count($pieces) - 1];
// result is: your_dir
George
  • 2,860
  • 18
  • 31
user2169219
  • 199
  • 1
  • 5
  • 3
    The OP seems to have already accepted an answer to their problem. Although you've clearly added value, do you need to vent your furstrations here? – Dutts Mar 14 '13 at 11:01
  • http://stackoverflow.com/help/formatting will explain how to make things **bold** avoiding the use of caps, which you have quite rightly pointed out denotes shouting on the internet. – Dutts Jun 27 '13 at 13:59
13
echo basename(__DIR__); will return the current directory name only
echo basename(__FILE__); will return the current file name only
Ameer Ul Islam
  • 380
  • 3
  • 5
7

Actually I found the best solution is the following:

$cur_dir = explode('\\', getcwd());
echo $cur_dir[count($cur_dir)-1];

if your dir is www\var\path\ Current_Path

then this returns Current_path

Andrew
  • 71
  • 1
  • 1
  • 4
    Watch out, in Linux servers we do not use backslash but the slash. PHP has a magic constant to make it cross platform, see `PATH_SEPARATOR` – renoirb Aug 22 '14 at 15:35
4

$myVar = str_replace('/', '', $_SERVER[REQUEST_URI]);

libs/images/index.php
Result: images

Oliver Nybo
  • 560
  • 1
  • 6
  • 24
Micky
  • 107
  • 3
2

To get the names of current directory we can use getcwd() or dirname(__FILE__) but getcwd() and dirname(__FILE__) are not synonymous. They do exactly what their names are. If your code is running by referring a class in another file which exists in some other directory then these both methods will return different results.

For example if I am calling a class, from where these two functions are invoked and the class exists in some /controller/goodclass.php from /index.php then getcwd() will return '/ and dirname(__FILE__) will return /controller.

Vishal Kumar Sahu
  • 1,232
  • 3
  • 15
  • 27
0

I use this line to get the actual file directory name without the path. (For windows)

substr(dirname(__FILE__), strrpos(dirname(__FILE__), '\\') + 1)

I changed it a bit to work on Linux also

substr(dirname(__FILE__), strrpos(str_replace('\\', '/', dirname(__FILE__)), '/') + 1)
Lars02_
  • 51
  • 6
0

basename(getcwd()); // returns only the current working dir name.

Wicher
  • 1
  • 1
    FYI: This question is rather old and a good and accepted answer has already been posted the very same day, including a comment with your exact answer: https://stackoverflow.com/a/9997400/801652 Be sure to read all the answers before posting one yourself with the same content – davethebrave Mar 04 '22 at 11:20