Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>__FILE__</code> is a <a href="http://php.net/manual/en/language.constants.predefined.php" rel="nofollow noreferrer">magic constant</a> containing the full path to the file you are executing. If you are inside an include, its path will be the contents of <code>__FILE__</code>.</p> <p>So with this setup: </p> <p><strong>/folder/random/foo.php</strong><br> </p> <pre><code>&lt;?php echo getcwd() . "\n"; echo dirname(__FILE__) . "\n" ; echo "-------\n"; include 'bar/bar.php'; </code></pre> <p><strong>/folder/random/bar/bar.php</strong> </p> <pre><code>&lt;?php echo getcwd() . "\n"; echo dirname(__FILE__) . "\n"; </code></pre> <p>You get this output: </p> <pre class="lang-none prettyprint-override"><code>/folder/random /folder/random ------- /folder/random /folder/random/bar </code></pre> <p>So <code>getcwd()</code> returns the directory where you started executing, while <code>dirname(__FILE__)</code> is file-dependent.</p> <p>On my webserver, <code>getcwd()</code> returns the location of the file that originally started executing. Using the CLI it is equal to what you would get if you executed <code>pwd</code>. This is supported by the <a href="https://secure.php.net/manual/en/features.commandline.differences.php" rel="nofollow noreferrer">documentation of the CLI SAPI</a> and a comment on the <code>getcwd</code> manual page:</p> <blockquote> <p>the CLI SAPI does - contrary to other SAPIs - NOT automatically change the current working directory to the one the started script resides in. </p> </blockquote> <p>So like: </p> <pre><code>thom@griffin /home/thom $ echo "&lt;?php echo getcwd() . '\n' ?&gt;" &gt;&gt; test.php thom@griffin /home/thom $ php test.php /home/thom thom@griffin /home/thom $ cd .. thom@griffin /home $ php thom/test.php /home </code></pre> <p>Of course, see also the manual at <a href="http://php.net/manual/en/function.getcwd.php" rel="nofollow noreferrer">http://php.net/manual/en/function.getcwd.php</a></p> <p><strong>UPDATE</strong>: Since PHP 5.3.0 you can also use the magic constant <code>__DIR__</code> which is equivalent to <code>dirname(__FILE__)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload