Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:27:33+00:00 2026-05-13T14:27:33+00:00

In PHP what is the difference between getcwd() dirname(__FILE__) They both return the same

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:27:34+00:00Added an answer on May 13, 2026 at 2:27 pm

    __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 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() . '\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__).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a function to return the difference between two dates <?php class days
Is there any difference between typing: <?php echo $_SERVER[REQUEST_URI] ?> or <?php echo $_SERVER['REQUEST_URI']
Is there a difference between NULL and null in PHP? Sometimes they seem to
Possible Duplicate: Multiple PHP tags Is there a difference between: <?php echo n ?>
What is the difference between shell_exec and system methods in PHP? Both take a
In PHP what is the difference between using \1 or $1 as $replacement in
Is there a difference between javascript cookies and php cookies?
Possible Duplicate: What is the difference between the PHP open tags <?= and <?php/<??
In PHP, What is the difference between declaring methods inside class like public function
Possible Duplicate: How to calculate the difference between two dates using PHP? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.