26

In PHP what is the difference between

getcwd()
dirname(__FILE__)

They both return the same result when I echo from CLI

echo getcwd()."\n";
echo dirname(__FILE__)."\n";

Returns:

/home/user/Desktop/testing/
/home/user/Desktop/testing/

Which is the best one to use? Does it matter? What to the more advanced PHP developers prefer?

hakre
  • 193,403
  • 52
  • 435
  • 836
Urda
  • 5,460
  • 5
  • 34
  • 47

3 Answers3

53

__FILE__ is a magic constant containing the full path to the file you are executing. If you are inside an include, its path will be the contents of __FILE__.

So with this setup:

/folder/random/foo.php

<?php
echo getcwd() . "\n";
echo dirname(__FILE__) . "\n" ;
echo "-------\n";
include 'bar/bar.php';

/folder/random/bar/bar.php

<?php
echo getcwd() . "\n";
echo dirname(__FILE__) . "\n";

You get this output:

/folder/random
/folder/random
-------
/folder/random
/folder/random/bar

So getcwd() returns the directory where you started executing, while dirname(__FILE__) is file-dependent.

On my webserver, getcwd() returns the location of the file that originally started executing. Using the CLI it is equal to what you would get if you executed pwd. This is supported by the documentation of the CLI SAPI and a comment on the getcwd manual page:

the CLI SAPI does - contrary to other SAPIs - NOT automatically change the current working directory to the one the started script resides in.

So like:

thom@griffin /home/thom $ echo "<?php echo getcwd() . '\n' ?>" >> test.php
thom@griffin /home/thom $ php test.php 
/home/thom
thom@griffin /home/thom $ cd ..
thom@griffin /home $ php thom/test.php 
/home

Of course, see also the manual at http://php.net/manual/en/function.getcwd.php

UPDATE: Since PHP 5.3.0 you can also use the magic constant __DIR__ which is equivalent to dirname(__FILE__).

user664833
  • 18,397
  • 19
  • 91
  • 140
Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
  • 3
    Perfect example is perfect! Thank you! – Urda Feb 02 '10 at 15:00
  • Is there a quick way to get the directory of the main script from the included script. So inside an included script which will be in a different directory, I want to know the directory of the main script that includes it. – CMCDragonkai May 29 '14 at 04:53
  • @CMCDragonkai: I don't know if there's a better way, but you could try setting a constant in the main file to the value of `__DIR__`. – Thom Wiggers May 29 '14 at 12:26
  • @ThomWiggers, Seems like `__DIR__` is more performant is that right? – Pacerier Jan 29 '15 at 05:33
  • I have no idea how they work internally, so I don't know. I doubt it matters much though and you should probably make that choice based on functionality. – Thom Wiggers Feb 01 '15 at 07:16
1

Try this.

Move your file into another directory say testing2.

This should be the result.

/home/user/Desktop/testing/
/home/user/Desktop/testing/testing2/

I would think getcwd is used for file operations, where dirname(__FILE__) uses the magic constant __FILE__ and uses the actual file path.


Edit: I was wrong.

Well you can change the working directory, with chdir.

So if you do that...

chdir('something');
echo getcwd()."\n";
echo dirname(__FILE__)."\n";

Those should be different.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Nope, this time it just echos /home/user/Desktop/testing/testing2 twice – Urda Feb 02 '10 at 14:46
  • OK, so let me be sure I got this... dirname(__FILE__) will *always* produce the location of the file in use. While getcwd will return the current working dir for the PHP process running (such as my command line script.) So assuming this, if I use a include("path/to/some/file/from/chdir/"); it should be able to find the referenced file? – Urda Feb 02 '10 at 14:54
1

If you call the file from the command line, the difference is clear.

cd foo
php bin/test.php

within test.php, getcwd() would return foo (your current working directory) and dirname(__FILE__) would return bin (the dirname of the file executing).

commonpike
  • 10,499
  • 4
  • 65
  • 58