1. <legend id='ZlPf1'><style id='ZlPf1'><dir id='ZlPf1'><q id='ZlPf1'></q></dir></style></legend>
  2. <small id='ZlPf1'></small><noframes id='ZlPf1'>

      <bdo id='ZlPf1'></bdo><ul id='ZlPf1'></ul>
  3. <tfoot id='ZlPf1'></tfoot>
    <i id='ZlPf1'><tr id='ZlPf1'><dt id='ZlPf1'><q id='ZlPf1'><span id='ZlPf1'><b id='ZlPf1'><form id='ZlPf1'><ins id='ZlPf1'></ins><ul id='ZlPf1'></ul><sub id='ZlPf1'></sub></form><legend id='ZlPf1'></legend><bdo id='ZlPf1'><pre id='ZlPf1'><center id='ZlPf1'></center></pre></bdo></b><th id='ZlPf1'></th></span></q></dt></tr></i><div id='ZlPf1'><tfoot id='ZlPf1'></tfoot><dl id='ZlPf1'><fieldset id='ZlPf1'></fieldset></dl></div>

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

      时间:2023-07-15
    2. <legend id='YmeYv'><style id='YmeYv'><dir id='YmeYv'><q id='YmeYv'></q></dir></style></legend>
    3. <small id='YmeYv'></small><noframes id='YmeYv'>

          <bdo id='YmeYv'></bdo><ul id='YmeYv'></ul>

        • <tfoot id='YmeYv'></tfoot>

              • <i id='YmeYv'><tr id='YmeYv'><dt id='YmeYv'><q id='YmeYv'><span id='YmeYv'><b id='YmeYv'><form id='YmeYv'><ins id='YmeYv'></ins><ul id='YmeYv'></ul><sub id='YmeYv'></sub></form><legend id='YmeYv'></legend><bdo id='YmeYv'><pre id='YmeYv'><center id='YmeYv'></center></pre></bdo></b><th id='YmeYv'></th></span></q></dt></tr></i><div id='YmeYv'><tfoot id='YmeYv'></tfoot><dl id='YmeYv'><fieldset id='YmeYv'></fieldset></dl></div>
                  <tbody id='YmeYv'></tbody>

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

                  <tbody id='RACUV'></tbody>
                  <bdo id='RACUV'></bdo><ul id='RACUV'></ul>

                  <tfoot id='RACUV'></tfoot>
                • <i id='RACUV'><tr id='RACUV'><dt id='RACUV'><q id='RACUV'><span id='RACUV'><b id='RACUV'><form id='RACUV'><ins id='RACUV'></ins><ul id='RACUV'></ul><sub id='RACUV'></sub></form><legend id='RACUV'></legend><bdo id='RACUV'><pre id='RACUV'><center id='RACUV'></center></pre></bdo></b><th id='RACUV'></th></span></q></dt></tr></i><div id='RACUV'><tfoot id='RACUV'></tfoot><dl id='RACUV'><fieldset id='RACUV'></fieldset></dl></div>

                        <small id='RACUV'></small><noframes id='RACUV'>

                        <legend id='RACUV'><style id='RACUV'><dir id='RACUV'><q id='RACUV'></q></dir></style></legend>