getcwd()和dirname(__ FILE__)之间的区别?我应该用哪个? [英] Difference Between getcwd() and dirname(__FILE__) ? Which should I use?
问题描述
在PHP中,
getcwd()
dirname(__ FILE__)
当我从CLI回显时,它们都返回相同的结果
echo getcwd()。\\\
;
echo dirname(__ FILE __)。\\\
;
返回:
code> / home / user / Desktop / testing /
/ home / user / Desktop / tests /
哪个是最好的使用?有关系吗?更高级的PHP开发人员喜欢什么?
__ FILE __
魔术常数包含正在执行的文件的完整路径。如果你在一个包含,它的路径将是 __ FILE __
的内容。
所以使用这个设置: / p>
/folder/random/foo.php
<?php
echo getcwd()。 \\\
;
echo dirname(__ FILE__)。 \\\
;
echo------- \\\
;
include'bar / bar.php';
/folder/random/bar/bar.php
<?php
echo getcwd()。 \\\
;
echo dirname(__ FILE__)。 \\\
;
你得到这个输出:
/ folder / random
/ folder / random
-------
/ folder / random
/ folder / random / bar
所以 getcwd()
返回您开始执行的目录,而 dirname(__ FILE __)
与文件相关。
在我的webserver, getcwd()
返回最初开始执行的文件的位置。使用CLI等于您执行 pwd
时会得到的结果。这是由 getcwd
手册页面的评论支持的:
CLI SAPI确实 - 与其他SAPI相反 - 不会自动将当前工作目录更改为已启动脚本所在的工作目录。
所以喜欢:
thom @ griffin / home / thom $ echo<?php echo getcwd()。'\\\
'?> >> test.php
thom @ griffin / home / thom $ php test.php
/ home / thom
thom @ griffin / home / thom $ cd ..
thom @ griffin /首页$ php thom / test.php
/ home
当然,另请参阅手册请访问 http://php.net/manual/en/function.getcwd.php
更新:由于PHP 5.3.0,您还可以使用魔术常数 __ DIR __
相当于 dirname(__ FILE __)
。
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?
__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 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__)
.
这篇关于getcwd()和dirname(__ FILE__)之间的区别?我应该用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!