Get current working directory of php script/application
Sometimes you need to know the full filesystem path of your PHP script — for example, when building file paths for includes or writing to a log file. There are two common ways to get it:
$myPath = realpath(dirname(__FILE__));
or
$myPath = getcwd();
The difference matters. dirname(__FILE__) gives you the directory of the file where the code is written, even if it’s included from somewhere else. getcwd() gives you the current working directory, which can change depending on how the script was called.
If you’re writing a library or an included file, use dirname(__FILE__) (or __DIR__ in PHP 5.3+) so the path is always relative to that file. Use getcwd() when you specifically need the directory the script was executed from.