getcwd()和dirname(__ FILE__)之间的区别?我应该使用哪个?

时间:2021-12-06 12:04:59

In PHP what is the difference between

在PHP中有什么区别

getcwd()
dirname(__FILE__)

They both return the same result when I echo from CLI

当我从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?

哪个是最好用的?有关系吗?更高级的PHP开发人员更喜欢什么?

3 个解决方案

#1


46  

__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__.

__FILE__是一个魔术常量,包含正在执行的文件的完整路径。如果你在include中,它的路径将是__FILE__的内容。

So with this setup:

所以使用这个设置:

/folder/random/foo.php

/folder/random/foo.php

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

/folder/random/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.

因此getcwd()返回您开始执行的目录,而dirname(__ FILE__)则依赖于文件。

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:

在我的web服务器上,getcwd()返回最初开始执行的文件的位置。使用CLI它等于你执行pwd时会得到的结果。 CLI SAPI的文档和getcwd手册页上的注释支持这一点:

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

CLI SAPI - 与其他SAPI相反 - 不会自动将当前工作目录更改为启动脚本所在的目录。

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

当然,另请参阅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__).

更新:从PHP 5.3.0开始,你也可以使用魔术常量__DIR__,它相当于dirname(__ FILE__)。

#2


1  

Try this.

尝试这个。

Move your file into another directory say testing2.

将文件移动到另一个目录,例如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.

我认为getcwd用于文件操作,其中dirname(__ FILE__)使用魔术常量__FILE__并使用实际文件路径。


Edit: I was wrong.

编辑:我错了。

Well you can change the working directory, with chdir.

那你可以用chdir改变工作目录。

So if you do that...

所以,如果你这样做......

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

Those should be different.

那些应该是不同的。

#3


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).

在test.php中,getcwd()将返回foo(当前工作目录)和dirname(__ FILE__)将返回bin(执行文件的dirname)。

#1


46  

__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__.

__FILE__是一个魔术常量,包含正在执行的文件的完整路径。如果你在include中,它的路径将是__FILE__的内容。

So with this setup:

所以使用这个设置:

/folder/random/foo.php

/folder/random/foo.php

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

/folder/random/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.

因此getcwd()返回您开始执行的目录,而dirname(__ FILE__)则依赖于文件。

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:

在我的web服务器上,getcwd()返回最初开始执行的文件的位置。使用CLI它等于你执行pwd时会得到的结果。 CLI SAPI的文档和getcwd手册页上的注释支持这一点:

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

CLI SAPI - 与其他SAPI相反 - 不会自动将当前工作目录更改为启动脚本所在的目录。

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

当然,另请参阅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__).

更新:从PHP 5.3.0开始,你也可以使用魔术常量__DIR__,它相当于dirname(__ FILE__)。

#2


1  

Try this.

尝试这个。

Move your file into another directory say testing2.

将文件移动到另一个目录,例如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.

我认为getcwd用于文件操作,其中dirname(__ FILE__)使用魔术常量__FILE__并使用实际文件路径。


Edit: I was wrong.

编辑:我错了。

Well you can change the working directory, with chdir.

那你可以用chdir改变工作目录。

So if you do that...

所以,如果你这样做......

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

Those should be different.

那些应该是不同的。

#3


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).

在test.php中,getcwd()将返回foo(当前工作目录)和dirname(__ FILE__)将返回bin(执行文件的dirname)。