Get PHP Current Folder – 6 easy and fast Solution

The first and the simplest is using getcwd php function (Gets the current working directory)

$current_directory = getcwd();

another three simple solutions using dirname php function  (Returns directory name component of path)

$current_directory = dirname(__FILE__); <=== The Fastest
//or
$current_directory = dirname($_SERVER["SCRIPT_FILENAME"]);
//or
$current_directory = dirname( $_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"] );

and two other quick solution using substr php function  (Return part of a string) and strrpos function (Find position of first occurrence of a string) :

$current_directory = substr(__FILE__, 0, strrpos(__FILE__, "\\"));
//or
$current_directory = substr( $_SERVER["SCRIPT_FILENAME"], 0, strrpos( $_SERVER["SCRIPT_FILENAME"], "/" ) );

Solutions can be used according to the context, each with its applications.

Happy coding!

byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *