Get the current directory: getcwd() vs dirname(__FILE__)

<?php
echo ‘getcwd(): ‘.getcwd();
echo ‘dirname(__FILE__): ‘.dirname(__FILE__);
?>

When opening the script in webbrowser then both commands will output the same path to the directory where the script is located in the file system.

However, when you include this script in another script (or call it via CLI from another directory) then the output will differ:

  • getcwd() will return the path to the calling script
  • dirname(__FILE__) will return the path to the included script

Tags:

Leave a Reply