get directory path in php code example
Example 1: dir name php
<?php
dirname(__FILE__);
?>
Example 2: php get file location
include __DIR__ . DIRECTORY_SEPARATOR . 'include.php';
echoScriptPath();
function echoScriptPath() {
list($scriptPath) = get_included_files();
echo 'The script being executed is ' . $scriptPath;
}
Example 3: php dirname
// dirname ( string $path [, int $levels = 1 ] ) : string
<?php
echo dirname("/etc/passwd") . PHP_EOL;
echo dirname("/etc/") . PHP_EOL;
echo dirname(".") . PHP_EOL;
echo dirname("C:\\") . PHP_EOL;
echo dirname("/usr/local/lib", 2);