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

2023-07-15php开发问题
1

本文介绍了getcwd() 和 dirname(__FILE__) 之间的区别?我应该使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

PHP 中的区别是什么

getcwd()目录名(__文件__)

当我从 CLI 回显时,它们都返回相同的结果

echo getcwd()."
";echo 目录名(__FILE__)."
";

返回:

/home/user/Desktop/testing//家/用户/桌面/测试/

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

解决方案

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

所以有了这个设置:

/folder/random/foo.php

/folder/random/bar/bar.php

你得到这个输出:

/folder/random/文件夹/随机-------/文件夹/随机/文件夹/随机/条

所以 getcwd() 返回你开始执行的目录,而 dirname(__FILE__) 是文件相关的.

在我的网络服务器上,getcwd() 返回最初开始执行的文件的位置.使用 CLI,它与执行 pwd 时得到的结果相同.这由 CLI SAPI 文档 和评论支持在 getcwd 手册页上:

<块引用>

CLI SAPI 确实 - 与其他 SAPI 不同 - 不会自动将当前工作目录更改为启动脚本所在的目录.

所以喜欢:

thom@griffin/home/thom $ echo "<?php echo getcwd() . '
' ?>">>测试.phpthom@griffin/home/thom $ php test.php/家/汤姆thom@griffin/home/thom $ cd ..汤姆@格里芬/home $ php 汤姆/test.php/家

当然,也请参见http://php.net/manual/en/function.getcwd.php

UPDATE:自 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()."
";
echo dirname(__FILE__)."
";

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() . "
";
echo dirname(__FILE__) . "
" ;
echo "-------
";
include 'bar/bar.php';

/folder/random/bar/bar.php

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

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() . '
' ?>" >> 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__) 之间的区别?我应该使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17