Samstag, 23. Dezember 2017

PHP get current directory name and file name

Get the current directory name and file name in PHP. There are several ways to get the name of the current directory and the current filename in PHP with different results. Below are some methods with their command and result listed.

Template:
echo command; || var_dump(command);
result as value || array || error_reporting

/var/www/html/testa/testb/test.php


echo getcwd();
/var/www/html/testa/testb


__DIR__ ( Magic constant )

echo __DIR__;
/var/www/html/testa/testb

echo getcwd(__DIR__);
[Warning: getcwd() expects exactly 0 parameters, ...]

echo dirname(__DIR__);
/var/www/html/testa

echo basename(__DIR__);
testb

echo realpath(__DIR__);
/var/www/html/testa/testb

var_dump(pathinfo(__DIR__));
array(3) {
  ["dirname"]=>
  string(43) "/var/www/html/testa"
  ["basename"]=>
  string(5) "testb"
  ["filename"]=>
  string(5) "testb"
}


__FILE__ ( Magic constant )

echo __FILE__;
/var/www/html/testa/testb/test.php

echo getcwd(__FILE__);
[Warning: getcwd() expects exactly 0 parameters, ...]

echo dirname(__FILE__);
/var/www/html/testa/testb

echo basename(__FILE__);
test.php

echo realpath(__FILE__);
/var/www/html/testa/testb/test.php

var_dump(pathinfo(__FILE__));
array(4) {
  ["dirname"]=>
  string(49) "/var/www/html/testa/testb"
  ["basename"]=>
  string(8) "test.php"
  ["extension"]=>
  string(3) "php"
  ["filename"]=>
  string(4) "test"
}


pathinfo(__FILE__)[]

echo pathinfo(__FILE__)["dirname"];
/var/www/html/testa/testb

echo pathinfo(__FILE__)["basename"];
test.php

echo pathinfo(__FILE__)["extension"];
php

echo pathinfo(__FILE__)["filename"];
test


dirname + preg_split

var_dump(preg_split("/\//", dirname(__FILE__)));
array(6) {
  [0]=>
  string(0) ""
  [1]=>
  string(3) "var"
  [2]=>
  string(3) "www"
  [3]=>
  string(7) "html"
  [4]=>
  string(5) "testa"
  [5]=>
  string(5) "testb"
}


dirname + preg_split + count - 1

echo preg_split("/\//", dirname(__FILE__))[count(preg_split("/\//", dirname(__FILE__))) - 1];
testb


realpath + preg_split

var_dump(preg_split("/\//", realpath(__FILE__)));
array(7) {
  [0]=>
  string(0) ""
  [1]=>
  string(3) "var"
  [2]=>
  string(3) "www"
  [3]=>
  string(7) "html"
  [4]=>
  string(5) "testa"
  [5]=>
  string(5) "testb"
  [6]=>
  string(8) "test.php"
}

realpath + preg_split + count - 1

preg_split("/\//", realpath(__FILE__))[count(preg_split("/\//", realpath(__FILE__))) - 1];
test.php

---

PHP source code

<?php

echo 'echo getcwd();<br>' . getcwd();
echo "<br>";

echo "<br>";
echo "<b>__DIR__ ( Magic constant )</b><br>";

echo "<br>";
echo "echo __DIR__;<br>";
echo __DIR__;
echo "<br>";

echo 'echo getcwd(__DIR__);<br>' . getcwd(__DIR__);
echo "<br>";
echo 'echo dirname(__DIR__);<br>' . dirname(__DIR__);
echo "<br><br>";
echo 'echo basename(__DIR__);<br>' . basename(__DIR__);
echo "<br><br>";
echo 'echo realpath(__DIR__);<br>' . realpath(__DIR__);
echo "<pre>";
echo 'var_dump(pathinfo(__DIR__));<br>';
var_dump(pathinfo(__DIR__));
echo "</pre>";

echo "<b>__FILE__ ( Magic constant )</b><br>";

echo "<br>";
echo "echo __FILE__;<br>";
echo __FILE__;
echo "<br>";

echo 'echo getcwd(__FILE__);<br>' . getcwd(__FILE__);
echo "<br>";
echo 'echo dirname(__FILE__);<br>' . dirname(__FILE__);
echo "<br><br>";
echo 'echo basename(__FILE__);<br>' . basename(__FILE__);
echo "<br><br>";
echo 'echo realpath(__FILE__);<br>' . realpath(__FILE__);
echo "<pre>";
echo 'var_dump(pathinfo(__FILE__));<br>';
var_dump(pathinfo(__FILE__));
echo "</pre>";

echo "pathinfo(__FILE__)[]";
echo "<br>";
echo 'echo pathinfo(__FILE__)["dirname"];<br>';
echo pathinfo(__FILE__)["dirname"];
echo "<br>";

echo "<br>";
echo 'echo pathinfo(__FILE__)["basename"];<br>';
echo pathinfo(__FILE__)["basename"];
echo "<br>";

echo "<br>";
echo 'echo pathinfo(__FILE__)["extension"];<br>';
echo pathinfo(__FILE__)["extension"];
echo "<br>";

echo "<br>";
echo 'echo pathinfo(__FILE__)["filename"];<br>';
echo pathinfo(__FILE__)["filename"];
echo "<br>";

echo "<br>";
echo "<pre>";
echo 'var_dump(preg_split("/\//", dirname(__FILE__)));<br>';
var_dump(preg_split("/\//", dirname(__FILE__)));
echo "</pre>";

echo "<br>";
echo 'echo preg_split("/\//", dirname(__FILE__))[count(preg_split("/\//", dirname(__FILE__))) - 1];<br>';
echo preg_split("/\//", dirname(__FILE__))[count(preg_split("/\//", dirname(__FILE__))) - 1];
echo "<br>";

echo "<br>";
echo "<pre>";
echo 'var_dump(preg_split("/\//", realpath(__FILE__)));<br>';
var_dump(preg_split("/\//", realpath(__FILE__)));
echo "</pre>";

echo "<br>";
echo 'preg_split("/\//", realpath(__FILE__))[count(preg_split("/\//", realpath(__FILE__))) - 1];<br>';
echo preg_split("/\//", realpath(__FILE__))[count(preg_split("/\//", realpath(__FILE__))) - 1];
echo "<br>";

?>

No guarantee. No warranty. No support.
Use at your own risk.